-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.php
More file actions
73 lines (59 loc) · 2.31 KB
/
Copy pathauth.php
File metadata and controls
73 lines (59 loc) · 2.31 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
include("./admin/config.php");
//database connection
session_start();
// Process login form submission
$email = sanitize_input($_POST['email']);
$password = sanitize_input($_POST['password']);
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$_SESSION['error']= "Invalid Email ";
header("Location: login.php");
exit();
}
if ( !isset($_POST['email'], $_POST['password']) ) {
// Could not get the data that should have been sent.
$_SESSION['error']= "Please fill both the username and password fields!";
header("Location: login.php");
exit();
}
// Retrieve user from database
$stmt = $conn->prepare("SELECT user_id, fname, user_status , email, pass, user_role , user_type FROM registered_user WHERE email = ? ");
if (!$stmt) {
die("Error preparing statement: " . mysqli_error($conn));
}
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// Account exists, now we verify the password
$row = $result->fetch_assoc();
$id = $row['user_id'];
$hashed_password = $row['pass'];
if($row['user_status']!=="active")
$_SESSION['error'] = "Your account is Inactive or Suspended. Please contact the vendor.";
if (password_verify($password, $hashed_password)) {
// Verification success! User has logged in
// Create sessions
$_SESSION['loggedin'] = true;
$_SESSION['user_id'] = $id;
$_SESSION['fname'] = $row['fname'];
$_SESSION['Role'] = $row['user_role'];
$_SESSION['Type'] = $row['user_type'];
// Redirect to the admin dashboard or any other page
if ($_SESSION['Role'] == 'admin') {
header("Location: admin/dashboard.php");
} else {
header("Location: home.php");
}
exit();
} else {
// Incorrect password
$_SESSION['error'] = "Invalid email or password.";
}
} else {
// Account not found
$_SESSION['error'] = " Account not found";
}
header("Location: login.php"); // Redirect back to login form with error (if not successful)
exit();
?>