-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.php
59 lines (50 loc) · 1.75 KB
/
admin.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
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Database connection settings (modify with your credentials)
$servername = "localhost";
$username = "Atharav";
$password = "Atharav@31";
$dbname = "project";
// Create a database connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check if the connection was successful
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve data from the form
$usernameOrEmail = $_POST["username"];
$password = $_POST["password"];
// Hash the password (assuming your passwords are stored as hashes)
$hashedPassword = hash("sha256", $password);
// Prepare and execute the SQL query
$sql = "SELECT * FROM users WHERE username = ? AND password = ?";
$stmt = $conn->prepare($sql);
if ($stmt === false) {
die("Prepare failed: " . $conn->error);
}
$stmt->bind_param("ss", $usernameOrEmail, $hashedPassword);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
// Authentication successful
// Check if the user has admin privileges (adjust the column name as per your database)
$userRow = $result->fetch_assoc();
if ($userRow["role"] === "admin") {
// Redirect to the admin panel
header("Location: admin.html");
exit();
} else {
// User does not have admin privileges
echo "You do not have admin privileges.";
}
} else {
// Authentication failed
echo "Login failed. Please check your credentials.";
}
// Close the database connection
$stmt->close();
$conn->close();
}else {
echo "Invalid request.";
}
?>