-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthenticate.php
52 lines (44 loc) · 1.44 KB
/
authenticate.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
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
require('db.php');
$username = $_POST['username'];
$password = $_POST['password'];
// Validate user input
if (empty($username) || empty($password)) {
// Handle empty fields
echo "Both username and password are required.";
exit();
}
// Query the database to retrieve user data
$sql = "SELECT id, username, password, is_admin FROM users WHERE username = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows == 1) {
$stmt->bind_result($id, $username, $hashed_password, $is_admin);
$stmt->fetch();
// Verify the password
if (password_verify($password, $hashed_password)) {
$_SESSION['id'] = $id;
$_SESSION['username'] = $username;
$_SESSION['is_admin'] = $is_admin;
if ($_SESSION['is_admin'] == 1) {
// Redirect to the admin panel for admin users
header("location: admin_panel.php");
} else {
// Redirect to a non-admin page for regular users
header("location: non_admin_page.php");
}
exit();
} else {
echo "Incorrect username or password.";
}
} else {
echo "User not found.";
}
$stmt->close();
$conn->close();
}
?>