-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforgot-password.php
154 lines (122 loc) · 5.11 KB
/
forgot-password.php
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
use PHPMailer\PHPMailer\PHPMailer;
require_once 'config/Database.php';
require 'vendor/autoload.php';
session_start();
// Handle forget password request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user_email = $_POST['email'];
$db = new Database();
$conn = $db->getConnection();
// Check if the user exists
$query = "SELECT id, email FROM ems.users WHERE email = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $user_email);
$stmt->execute();
$stmt->bind_result($user_id, $email);
$stmt->fetch();
$stmt->close();
if ($user_id) {
// Generate a unique token
$token = bin2hex(random_bytes(32));
// Store the token in the database
$query = "INSERT INTO ems.password_reset_tokens (user_id, token, expiration_time) VALUES (?, ?, ?)";
$stmt = $conn->prepare($query);
$expirationTime = date('Y-m-d H:i:s', strtotime('+1 hour')); // Token expires in 1 hour
$stmt->bind_param('iss', $user_id, $token, $expirationTime);
$stmt->execute();
$stmt->close();
// Send password reset email
$resetLink = "http://localhost/EMS/reset-password.php?token=$token"; // Use localhost
$subject = "Password Reset";
$message = "Click the following link to reset your password: $resetLink";
// Use PHPMailer to send the email
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Change to your SMTP server
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // Your Gmail username or SMTP username
$mail->Password = 'dvnv eusa qkuh pqab'; // SMTP password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->Debugoutput = 'html'; // Set debug output format to HTML
// Recipients
$mail->setFrom('[email protected]', 'Israr Administration'); // Change to your email
$mail->addAddress($email);
// Content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
$_SESSION['success_message'] = "Password reset instructions sent to your email.";
} catch (Exception $e) {
// Log the error (do not expose to users in production)
error_log("Email could not be sent. Mailer Error: {$mail->ErrorInfo}");
$_SESSION['error_message'] = "Email could not be sent. Please try again later.";
}
} else {
$_SESSION['error_message'] = "User not found.";
}
// Redirect to the login page
header("Location: forgot-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>forget password</title>
<link rel="stylesheet" href="asset/css/bootstrap.min.css">
<style>
.btn-color{
background-color: #0e1c36;
color: #fff;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-6">
<div class="card mt-5">
<div class="card-header">
Forget Password
</div>
<div class="card-body">
<?php if (!empty($_SESSION['success_message'])) : ?>
<div class="alert alert-success" role="alert">
<?php echo $_SESSION['success_message']; ?>
</div>
<?php unset($_SESSION['success_message']); ?>
<?php endif; ?>
<?php if (!empty($_SESSION['error_message'])) : ?>
<div class="alert alert-danger" role="alert">
<?php echo $_SESSION['error_message']; ?>
</div>
<?php unset($_SESSION['error_message']); ?>
<?php endif; ?>
<form method="post" action="forgot-password.php">
<div class="mb-3">
<label for="forgot-email" class="form-label">Enter your email to reset password:</label>
<input type="email" class="form-control" id="forgot-email" name="email" required>
</div>
<button type="submit" class="btn btn-primary px-5 mb-2 w-100">Reset Password</button>
<a href="index.php" class="btn btn-color px-5 w-100">Go Sign in !!</a>
</form>
<!-- The rest of your HTML remains the same -->
</div>
</div>
<div class="col-md-3">
</div>
</div>
</div>
</div>
</body>
</html>