-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_process.php
More file actions
93 lines (83 loc) · 2.9 KB
/
Copy pathauth_process.php
File metadata and controls
93 lines (83 loc) · 2.9 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
session_start();
require_once 'config.php';
// Initialize alerts array if not exists
if (!isset($_SESSION['alerts'])) {
$_SESSION['alerts'] = [];
}
// --- REGISTRATION LOGIC ---
if (isset($_POST['register_btn'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
// 1. Check if email already exists using a Prepared Statement
$stmt = $conn->prepare("SELECT email FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$_SESSION['alerts'][] = [
'type' => 'error',
'message' => 'This email is already registered!'
];
$_SESSION['active_form'] = 'register';
} else {
// 2. Insert new user safely
$insert_stmt = $conn->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
$insert_stmt->bind_param("sss", $name, $email, $password);
if ($insert_stmt->execute()) {
$_SESSION['alerts'][] = [
'type' => 'success',
'message' => 'Registration successful! Please login.'
];
$_SESSION['active_form'] = 'login';
} else {
$_SESSION['alerts'][] = [
'type' => 'error',
'message' => 'Something went wrong. Please try again.'
];
}
$insert_stmt->close();
}
$stmt->close();
header('Location: index.php');
exit();
}
// --- LOGIN LOGIC ---
if (isset($_POST['login_btn'])) {
$email = trim($_POST['email']);
$password = $_POST['password'];
// 3. Select user safely using a Prepared Statement
$stmt = $conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$user = $result->fetch_assoc();
// 4. Verify the hashed password
if (password_verify($password, $user['password'])) {
$_SESSION['name'] = $user['name'];
$_SESSION['alerts'][] = [
'type' => 'success',
'message' => 'Welcome back, ' . $user['name'] . '!'
];
unset($_SESSION['active_form']); // Hide the modal
} else {
$_SESSION['alerts'][] = [
'type' => 'error',
'message' => 'Incorrect password!'
];
$_SESSION['active_form'] = 'login';
}
} else {
$_SESSION['alerts'][] = [
'type' => 'error',
'message' => 'No account found with that email.'
];
$_SESSION['active_form'] = 'login';
}
$stmt->close();
header('Location: index.php');
exit();
}
?>