Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/projects/slutprojekt/
css/
endpoints/
fail.php
functions/
img/
incl/
js/
logged_in/
login.php
resetpw.php
restaurant/
setup/
resetpw.php
56 lines UTF-8 Windows (CRLF)
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
<?php
session_start();
require __DIR__ . '/../../dbconnect.php';
require_once __DIR__ . '/functions/test_inputs.php';
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['token'])) {
$token = $_GET['token'];
$stmt = $dbconn->prepare('SELECT email FROM slutprojekt_hungry_users WHERE login_token = ?');
$stmt->execute([$token]);
$user = $stmt->fetch();
if ($user) {
?>
<form method="POST">
<input type="hidden" name="token" value="<?php echo htmlspecialchars($token); ?>">
<label for="password">Nytt lösenord:</label>
<input type="password" name="password" required>
<label for="confirm_password">Bekräfta lösenord:</label>
<input type="password" name="confirm_password" required>
<button type="submit">Återställ lösenord</button>
</form>
<?php
} else {
echo 'Ogiltig token.';
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['token'], $_POST['password'], $_POST['confirm_password'])) {
$token = $_POST['token'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
if ($password !== $confirm_password) {
echo 'Lösenorden matchar inte.';
exit;
}
$stmt = $dbconn->prepare('SELECT email FROM slutprojekt_hungry_users WHERE login_token = ?');
$stmt->execute([$token]);
$user = $stmt->fetch();
if ($user) {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $dbconn->prepare('UPDATE slutprojekt_hungry_users SET password = ? WHERE login_token = ?');
$stmt->execute([$hashed_password, $token]);
header("Location: endpoints/login.php?token=$token");
} else {
echo 'Ogiltig token.';
}
} else {
echo 'Ogiltig begäran.';
}