Källkodsbrowser

change_password.php

92 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
<?php
require 'check_login.php';
$user $_SESSION['username'];
$filePath __DIR__ "/users.txt";
$msg '';

if (
$_SERVER['REQUEST_METHOD'] === 'POST') {
    
$old $_POST['old'];
    
$new $_POST['new'];
    
$lines file($filePathFILE_IGNORE_NEW_LINES);
    foreach (
$lines as &$line) {
        list(
$u$p$r) = explode(";"$line);
        if (
$u === $user && $p === $old) {
            
$line "$u;$new;$r";
            
file_put_contents($filePathimplode("\n"$lines) . "\n");
            
$msg "Lösenordet har ändrats!";
            break;
        }
    }
    if (!
$msg)
        
$msg "Fel nuvarande lösenord.";
}
?>
<!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>