Visa Källkod

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

NamnTypStorlek
actions.phpPHP Fil8.5 KB
admin.phpPHP Fil11.1 KB
auth.phpPHP Fil4.5 KB
change_password.phpPHP Fil3.4 KB
check_login.phpPHP Fil211 B
config.phpPHP Fil411 B
index.phpPHP Fil4 KB

auth.php

128 lines UTF-8 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
session_start
();
require_once 
'config.php';
$action $_GET['action'] ?? '';

global 
$pdo;

switch (
$action) {
    case 
'register':
            
// Registrera ny användare
            
$username trim($_POST['username'] ?? '');
            
$email trim($_POST['email'] ?? '');
            
$password $_POST['password'] ?? '';

            
// Validera fält
            
if (empty($username) || empty($email) || empty($password)) {
                
header("Location: index.php?error=empty");
                exit;
            }

            
// Kontrollera om användarnamn eller email redan finns
            
$Qstmts $pdo->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
            
$Qstmts->execute([$username$email]);
            if (
$Qstmts->fetch()) {
                
header("Location: index.php?error=exists");
                exit;
            }

            
// Skapa användare med hashat lösenord
            
$passwordHash password_hash($passwordPASSWORD_BCRYPT);
            
$Qstmts $pdo->prepare("
                INSERT INTO users (username, email, password_hash, role, created_at) 
                VALUES (?, ?, ?, 'user', NOW())
            "
);
            
$Qstmts->execute([$username$email$passwordHash]);
            
$userId $pdo->lastInsertId();

            
// Skapa huvudkonto med startsaldo
            
$Qstmts $pdo->prepare("
                INSERT INTO accounts (user_id, account_name, balance, created_at) 
                VALUES (?, 'Huvudkonto', 1000.00, NOW())
            "
);
            
$Qstmts->execute([$userId]);
            
$accountId $pdo->lastInsertId();

            
// Skapa starttransaktion
            
$Qstmts $pdo->prepare("
                INSERT INTO transactions (user_id, account_id, type, amount, date) 
                VALUES (?, ?, 'account-open', 1000.00, NOW())
            "
);
            
$Qstmts->execute([$userId$accountId]);

            
header("Location: index.php?success=registered");
            exit;

        case 
'login':
            
// Logga in användare
            
$username trim($_POST['username'] ?? '');
            
$password $_POST['password'] ?? '';
            
$remember = isset($_POST['remember']);

            if (empty(
$username) || empty($password)) {
                
header("Location: index.php?error=invalid");
                exit;
            }

            
// Hämta användare
            
$Qstmts $pdo->prepare("SELECT * FROM users WHERE username = ?");
            
$Qstmts->execute([$username]);
            
$user $Qstmts->fetch();

            if (!
$user) {
                
header("Location: index.php?error=invalid");
                exit;
            }

            
// Verifiera lösenord
            
if (!password_verify($password$user['password_hash'])) {
                
header("Location: index.php?error=invalid");
                exit;
            }

            
// Uppdatera senaste inloggning
            
$Qstmts $pdo->prepare("UPDATE users SET last_login = NOW() WHERE id = ?");
            
$Qstmts->execute([$user['id']]);

            
// Spara cookie om "Håll mig inloggad" är ikryssad
            
if ($remember) {
                
setcookie('username'$user['username'], time() + (30 24 60 60), '/');
            }

            
$_SESSION['user_id'] = $user['id'];
            
$_SESSION['username'] = $user['username'];
            
$_SESSION['role'] = $user['role'];
            
$_SESSION['loggedin'] = true;

            
header("Location: admin.php");
            exit;

        case 
'logout':
            
// Logga ut användare
            
setcookie('username'''time() - 3600'/');
            
session_destroy();
            
header("Location: index.php");
            exit;

        case 
'delete':
            
// Radera användare och tillhörande data
            
if (!isset($_SESSION['user_id'])) {
                
header("Location: index.php");
                exit;
            }

            
$userId $_SESSION['user_id'];
            
            
// CASCADE DELETE tar bort accounts + transactions automatiskt
            
$Qstmts $pdo->prepare("DELETE FROM users WHERE id = ?");
            
$Qstmts->execute([$userId]);

            
setcookie('username'''time() - 3600'/');
            
session_destroy();
            
header("Location: index.php?success=deleted");
            exit;

        default:
            
header("Location: index.php");
            exit;
}