-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.php
More file actions
59 lines (49 loc) · 1.85 KB
/
process.php
File metadata and controls
59 lines (49 loc) · 1.85 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
<?php
// Database connection
$servername = "localhost";
$username = "root"; // Your database username
$password = ""; // Your database password
$dbname = "edtech"; // Your database name
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Processing the login form
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = trim($_POST['email']);
$password = trim($_POST['password']);
// Validate input
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<script>alert('Invalid email format!');</script>";
echo "<script>window.location.href = 'login.php';</script>";
exit;
}
// Prepare and execute the query
$stmt = $conn->prepare("SELECT id, fullname, password FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$user = $result->fetch_assoc();
// Verify password
if (password_verify($password, $user['password'])) {
// Successful login
session_start();
$_SESSION['user_id'] = $user['id'];
$_SESSION['user_name'] = $user['fullname'];
echo "<script>alert('Login successful!'); window.location.href = 'dashboard.php';</script>";
} else {
// Incorrect password
echo "<script>alert('Invalid email or password.');</script>";
echo "<script>window.location.href = 'login.php';</script>";
}
} else {
// Email not found
echo "<script>alert('No account found with this email.');</script>";
echo "<script>window.location.href = 'login.php';</script>";
}
$stmt->close();
}
$conn->close();
?>