-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_login.php
47 lines (39 loc) · 1.43 KB
/
process_login.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
<?php
$is_invalid = false;
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$conn = require __DIR__ . "/db_connection.php";
// Get the email and password from POST request
$email = $_POST["email"];
$password = $_POST["password"];
// Prepare and execute query to get user details using a prepared statement
$stmt = $conn->prepare("SELECT user_id, name, profile_image, password_hash FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($user = $result->fetch_assoc()) {
// Verify the password
if (password_verify($password, $user["password_hash"])) {
session_start();
session_regenerate_id();
// Set session variables
$_SESSION["user_id"] = $user["user_id"];
$_SESSION['user_name'] = $user['name'];
$_SESSION['profile_image_url'] = $user['profile_image'] ? $user['profile_image'] : 'default-profile-image.jpg';
// Return success response
echo json_encode(['status' => 'success']);
exit;
} else {
$is_invalid = true;
}
} else {
$is_invalid = true;
}
$stmt->close();
$conn->close();
// Return error response
if ($is_invalid) {
echo json_encode(['status' => 'error', 'message' => 'Invalid email or password.']);
exit;
}
}
?>