Källkodsbrowser

change_password.php

109 lines UTF-8 Unix (LF)
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
<?php
require 'check_login.php';
require 
'db_connect.php';
$user $_SESSION['username'];

$msg '';

if (
$_SERVER['REQUEST_METHOD'] === 'POST') {
    
$old $_POST['old'];
    
$new $_POST['new'];

    try {
        
// Hämta användarens nuvarande lösenord-hash
        
$stmt $pdo->prepare("SELECT password FROM users WHERE username = ?");
        
$stmt->execute([$user]);
        
$user_data $stmt->fetch();

        if (
$user_data && password_verify($old$user_data['password'])) {
            
// Verifiera att det nya lösenordet inte är tomt
            
if (empty($new)) {
                
$msg "Det nya lösenordet får inte vara tomt.";
            } else {
                
// Hasha det nya lösenordet
                
$hashed_new_password password_hash($newPASSWORD_DEFAULT);

                
// Uppdatera lösenordet i databasen
                
$stmt $pdo->prepare("UPDATE users SET password = ? WHERE username = ?");
                
$stmt->execute([$hashed_new_password$user]);

                
$msg "Lösenordet har ändrats!";
            }
        } else {
            
$msg "Fel nuvarande lösenord.";
        }
    } catch (
\PDOException $e) {
        
error_log("Database error in change_password: " $e->getMessage());
        
$msg "Ett fel uppstod vid lösenordsbyte.";
    }
}
?>
<!DOCTYPE html>
<html lang="sv">

<head>
    <meta charset="UTF-8">
    <title>Byt lösenord</title>
    <style>
        body {
            font-family: Arial;
            background: #f0f2f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .box {
            background: white;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            width: 350px;
            text-align: center;
        }

        input,
        button {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border-radius: 5px;
            border: 1px solid #ccc;
        }

        button {
            background: #1b91f8;
            color: white;
            border: none;
            cursor: pointer;
        }

        button:hover {
            background: #0268dc;
        }

        a {
            display: inline-block;
            margin-top: 10px;
            color: #1b91f8;
            text-decoration: none;
        }
    </style>
</head>

<body>
    <div class="box">
        <h2>Byt lösenord</h2>
        <?php if ($msg): ?>
            <p><?= htmlspecialchars($msg?></p><?php endif; ?>
        <form method="post">
            <input type="password" name="old" placeholder="Nuvarande lösenord" required>
            <input type="password" name="new" placeholder="Nytt lösenord" required>
            <button type="submit">Byt lösenord</button>
        </form>
        <a href="user.php">Tillbaka</a>
    </div>
</body>

</html>