-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
45 lines (40 loc) · 1.35 KB
/
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
<?php
session_start();
include 'config.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
$password = $_POST['password'];
// Query the database to check if the user exists
$sql = "SELECT * FROM users WHERE name = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$stored_password = $row['password'];
// Verify the hashed password
if (password_verify($password, $stored_password)) {
$_SESSION['username'] = $username;
// Check if the user is an admin
if ($username == 'admin') {
header("Location: admin_dashboard.php");
exit();
} else {
header("Location: user_dashboard.php");
exit();
}
} else {
// Incorrect password
echo '<script>alert("Incorrect password.")</script>';
echo '<script>window.location.href = "index.php";</script>';
exit();
}
} else {
// User does not exist
echo '<script>alert("User does not exist.")</script>';
echo '<script>window.location.href = "index.php";</script>';
exit();
}
}
$conn->close();