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

change_password.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
();
// Sätt lokal tidszon
if (function_exists('date_default_timezone_set')) {
    @
date_default_timezone_set('Europe/Stockholm');
}
// inkludera de filer
require_once 'check_login.php'// kontrollera om användaren är inloggad
require_once 'json_store.php';


// Om formuläret skickats via POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// 1) Läs in lösenordsfält via POST
    
$currentPassword $_POST['current_password'] ?? '';
    
$newPassword $_POST['new_password'] ?? '';
    
$confirmPassword $_POST['confirm_password'] ?? '';

// 2) Validering; kontrollera att alla fält är ifyllda
    
if (empty($currentPassword) || empty($newPassword) || empty($confirmPassword)) {
        
header("Location: change_password.php?error=empty");
        exit;
    }
// Kontrollera att nytt lösenord matchar bekräftelsen
    
if ($newPassword !== $confirmPassword) {
        
header("Location: change_password.php?error=mismatch");
        exit;
    }


// 3) Hämta aktuell användare från session och users.json
    
$uname $_SESSION['username'] ?? null;
    if (!
$uname) {
        
header("Location: index.php?noaccess=1");
        exit;
    }

    
$users load_users();
    
$foundIdx null;
// Leta upp aktuell användare i arrayen
    
foreach ($users as $index => $user) {
        if ((
$user['username'] ?? null) === $uname) {
            
$foundIdx $index;
            break;
        }
    }
    if (
$foundIdx === null) {
        
header("Location: index.php?noaccess=1");
        exit;
    }

    
$user $users[$foundIdx];
// 4) Verifiera nuvarande lösenord
    
if (!isset($user['password']) || !password_verify($currentPassword$user['password'])) {
        
header("Location: change_password.php?error=wrong");
        exit;
    }

// 5) Hasha och spara nytt lösenord i users.json
    
$hashedPassword password_hash($newPasswordPASSWORD_BCRYPT);
    
$users[$foundIdx]['password'] = $hashedPassword;

    if (!
save_users($users)) {
        
header("Location: change_password.php?error=unknown");
        exit;
    }

// 6) Ta bort ev. flagga (om den finns i sessionen)
    
unset($_SESSION['must_change_password']);

// lyckad ändring > skicka till admin.php
    
header("Location: admin.php?success=password_changed");
    exit;
}


// Kontrollera om lösenordsbyte är obligatoriskt (flagga i GET eller session)
$isRequired = isset($_GET['required']) || isset($_SESSION['must_change_password']);
?>
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ändra lösenord</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="pwchange-wrap">
    <h2>Ändra lösenord</h2>

    <?php if ($isRequired): ?>
        <div class="notice warning">
            Du måste byta lösenord innan du kan fortsätta.
        </div>
    <?php endif; ?>

    <?php if (isset($_GET['error'])): ?>
        <div class="notice error">
            <?php
            
if ($_GET['error'] === 'empty') {
                echo 
"Fyll i alla fält!";
            } elseif (
$_GET['error'] === 'mismatch') {
                echo 
"Nya lösenorden matchar inte!";
            } elseif (
$_GET['error'] === 'wrong') {
                echo 
"Felaktigt nuvarande lösenord!";
            } else {
                echo 
"Ett fel uppstod. Försök igen.";
            }
            
?>
        </div>
    <?php endif; ?>

    <form method="post">
        <input type="password" name="current_password" placeholder="Nuvarande lösenord" required>
        <input type="password" name="new_password" placeholder="Nytt lösenord" required>
        <input type="password" name="confirm_password" placeholder="Bekräfta nytt lösenord" required>
        <button type="submit">Ändra lösenord</button>
    </form>

    <?php if (!$isRequired): ?>
        <p style="text-align: center; margin-top: 15px;">
            <a href="admin.php" style="color: #3b82f6; text-decoration: none;">← Tillbaka till kontrollpanelen</a>
        </p>
    <?php endif; ?>
</div>
</body>
</html>