Visa Källkod

The following files exist in this folder. Click to view.

NamnTypStorlek
actions.phpPHP Fil7.9 KB
admin.phpPHP Fil13.7 KB
auth.phpPHP Fil4.5 KB
change_password.phpPHP Fil4.1 KB
check_login.phpPHP Fil911 B
index.phpPHP Fil4 KB
json_store.phpPHP Fil2.1 KB
transactions.jsonJSON Fil4.6 KB
users.jsonJSON Fil1.5 KB

index.php

112 lines ISO-8859-1 BOM Windows (CRLF) - Type: PHP Fil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
session_start
();
if (
function_exists('date_default_timezone_set')) {
    @
date_default_timezone_set('Europe/Stockholm');
}

// Auto-inloggning från cookie
if (!isset($_SESSION['loggedin']) && isset($_COOKIE['username'])) {
    
// Verifiera att användaren finns i users.json
    
require_once 'json_store.php';
    
$users load_users();
    
$userExists false;
    
$userRole 'user';
    
    foreach (
$users as $u) {
        if ((
$u['username'] ?? null) === $_COOKIE['username']) {
            
$userExists true;
            
$userRole $u['role'] ?? 'user';
            break;
        }
    }
    
    if (
$userExists) {
        
$_SESSION['username'] = $_COOKIE['username'];
        
$_SESSION['role'] = $userRole;
        
$_SESSION['loggedin'] = true;
        
header("Location: admin.php");
        exit;
    }
}

// Redirigera om redan inloggad
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
    
header("Location: admin.php");
    exit;
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Inloggningsapplikation</title>
    <link rel="stylesheet" href="style.css">
    <link rel="icon" href="../m02/Favicon-a.jpg" type="image/x-icon">
</head>
<body>
<div class="center-wrap">
    <div>
        <?php
        
//   Konto raderat (meddelande)
        
if (isset($_GET['success']) && $_GET['success'] === 'deleted') {
            echo 
'<div class="notice error" id="top-notice">Ditt konto har raderats.</div>';
        }

        
//   Registrering klar (meddelande)
        
if (isset($_GET['success']) && $_GET['success'] === 'registered') {
            echo 
'<div class="notice success" id="top-notice">Konto skapat! Du kan nu logga in.</div>';
        }

        
//   FEL: Visa kort feltext baserat på query
        
if (isset($_GET['error'])): ?>
            <div class="notice error" id="top-notice">
                <?php
                
if ($_GET['error'] === 'locked') {
                    echo 
"Kontot är låst på grund av för många misslyckade inloggningar. Kontakta admin.";
                } elseif (
$_GET['error'] === 'invalid') {
                    echo 
"Felaktigt användarnamn eller lösenord.";
                } elseif (
$_GET['error'] === 'exists') {
                    echo 
"Användarnamnet finns redan.";
                } elseif (
$_GET['error'] === 'empty') {
                    echo 
"Fyll i alla fält.";
                } else {
                    echo 
"Ett fel uppstod. Försök igen.";
                }
                
?>
            </div>
        <?php endif;

        
//   Otillåten åtkomst
        
if (isset($_GET['noaccess'])) {
            echo 
'<div class="notice warning" id="top-notice">Du har inte behörighet att komma åt den sidan.</div>';
        }
        
?>
    </div>

    <div id="frm1">
        <!-- Inloggningsformulär -->
        <form action="auth.php?action=login" method="post">
            <h3 style="font-size: 1.6rem"> Logga In</h3>
            <input type="text" name="username" placeholder="Användarnamn" required>
            <input type="password" name="password" placeholder="Lösenord" required>
            <div class="chk-wrap">
                <label for="chk">
                    <input type="checkbox" name="remember" id="chk">
                    <span class="check">Håll mig inloggad</span>
                </label>
            </div>
            <button type="submit">Login</button>
        </form>
        <hr>
        <!-- Registreringsformulär (skapar startsaldo 1000 kr via transaktion) -->
        <form action="auth.php?action=register" method="post">
            <h3>Skapa nytt konto</h3>
            <input type="text" name="username" placeholder="Nytt användarnamn" required>
            <input type="password" name="password" placeholder="Nytt lösenord" required>
            <button type="submit">Skapa konto</button>
        </form>
    </div>
</div>
</body>
</html>