-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforget_password.php
More file actions
101 lines (89 loc) · 3.45 KB
/
Copy pathforget_password.php
File metadata and controls
101 lines (89 loc) · 3.45 KB
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
<?php
session_start();
require_once 'mysql.php';
require_once 'functions.php';
$message = '';
$is_error = false;
if (isset($_POST['submit'])) {
$username = trim($_POST['username'] ?? '');
if ($username === '') {
$message = 'Username is required.';
$is_error = true;
} else {
$random_token = random_token();
try {
$stmt = $connection->prepare('SELECT * FROM users WHERE username = :username LIMIT 1');
$stmt->execute([':username' => $username]);
$user = $stmt->fetch();
} catch (PDOException $e) {
$message = 'Database error while checking username.';
$is_error = true;
}
if (!$user && !$is_error) {
$message = 'Username not found.';
$is_error = true;
}
if (!$is_error) {
try {
$stmt = $connection->prepare('UPDATE users SET reset_token = :token WHERE username = :username');
$stmt->execute([
':token' => $random_token,
':username' => $username,
]);
} catch (PDOException $e) {
header('Location: msg.php?msg=Failed to send reset token&type=error&goto=forget_password.php');
exit();
}
if ($stmt->rowCount() > 0) {
$reset_token_url = 'http://owaspzero-x.ir/reset_password.php?token=' . urlencode($random_token);
$to = $user['email'];
$subject = 'Reset Password';
$messageBody = 'Click the link below to reset your password: ' . $reset_token_url;
$headers = 'From: OWASPZero-X <no-reply@owaspzero-x.ir>';
mail($to, $subject, $messageBody, $headers);
header('Location: msg.php?msg=Reset token sent to email&type=success&goto=login.php');
exit();
}
header('Location: msg.php?msg=Failed to send reset token&type=error&goto=forget_password.php');
exit();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Forgot Password</title>
<link rel="stylesheet" href="//static.owaspzero-x.ir/styles.css">
</head>
<body>
<div class="home-link">
<a class="btn" href="/index.php"><span class="icon">⌂</span>Home</a>
</div>
<div class="card">
<h1>Forgot Password</h1>
<p>Enter your username to request a reset link.</p>
<?php if ($message !== ''): ?>
<p class="message <?php echo $is_error ? 'error' : 'success'; ?>">
<?php echo htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); ?>
</p>
<?php endif; ?>
<form method="POST" action="/forget_password.php">
<div class="form-group">
<label for="username">Username</label>
<input id="username" name="username" type="text"
value="<?php echo isset($username) ? htmlspecialchars($username, ENT_QUOTES, 'UTF-8') : ''; ?>" required />
</div>
<div class="form-group">
<button type="submit" name="submit" value="1">Send Reset Link</button>
</div>
</form>
<div class="links links-compact">
<a href="/login.php">Back to login</a>
<a href="/register.php">Create account</a>
</div>
</div>
</body>
</html>