-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforgot_password.php
More file actions
116 lines (94 loc) · 3.65 KB
/
forgot_password.php
File metadata and controls
116 lines (94 loc) · 3.65 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
session_start();
// MySQL database configuration
$servername = "localhost";
$username = "root";
$password = "9586";
$dbname = "penny_auction";
// Create a new MySQL connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Handle form submission
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$email = $_POST["email"];
// Generate a new password
$newPassword = generateRandomPassword();
// Hash the new password
$hashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);
// Update the user's password in the database
$sql = "UPDATE users SET password = '$hashedPassword' WHERE email = '$email'";
$result = $conn->query($sql);
if ($result === TRUE) {
// Password updated successfully
// Send the new password to the user's email
sendNewPasswordByEmail($email, $newPassword);
// Redirect to the login page with a success message
header("Location: login.php?success=1");
exit();
} else {
// Error occurred while updating the password
$errorMessage = "Failed to update password. Please try again.";
}
}
// Close the database connection
$conn->close();
// Function to generate a random password
function generateRandomPassword($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$password = '';
for ($i = 0; $i < $length; $i++) {
$password .= $characters[rand(0, strlen($characters) - 1)];
}
return $password;
}
// Function to send the new password to the user's email
function sendNewPasswordByEmail($email, $newPassword) {
// Use your preferred method to send an email with the new password to the user
// For example, you can use PHPMailer or the built-in mail() function
// Here's an example using the mail() function:
$subject = "New Password for Your Account";
$message = "Your new password is: $newPassword";
$headers = "From: your-email@example.com" . "\r\n" .
"Reply-To: your-email@example.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($email, $subject, $message, $headers);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Penny Auction Website - Forgot Password</title>
<!-- Add your SEO meta tags here -->
<!-- Import necessary scripts -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
</head>
<body>
<!-- Header -->
<?php include 'header.php'; ?>
<div class="container">
<h1>Forgot Password</h1>
<?php if (isset($errorMessage)) { ?>
<div class="alert alert-danger" role="alert">
<?php echo $errorMessage; ?>
</div>
<?php } ?>
<!-- Forgot password form -->
<form method="POST">
<!-- Form fields -->
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<button type="submit" class="btn btn-primary">Reset Password</button>
</form>
</div>
<!-- Footer -->
<?php include 'footer.php'; ?>
</body>
</html>