-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.php
More file actions
67 lines (55 loc) · 2.26 KB
/
Copy pathauth.php
File metadata and controls
67 lines (55 loc) · 2.26 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
<?php
session_start();
// Enable error reporting for development (disable in production)
error_reporting(E_ALL);
ini_set('display_errors', 1);
include 'config.php';
// Establish database connection
$conn = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(json_encode(['success' => false, 'message' => 'Database connection failed: ' . $conn->connect_error]));
}
// Check if POST request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Retrieve and sanitize input
$inputUsername = isset($_POST['username']) ? trim($_POST['username']) : '';
$inputPassword = isset($_POST['password']) ? trim($_POST['password']) : '';
// Basic input validation
if (empty($inputUsername) || empty($inputPassword)) {
die(json_encode(['success' => false, 'message' => 'Username and password are required.']));
}
// Prepare the SQL statement to fetch the user
$stmt = $conn->prepare("SELECT id, username, password, dispname FROM users WHERE username = ? LIMIT 1");
if ($stmt) {
$stmt->bind_param("s", $inputUsername); // Bind the username parameter
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
// Fetch the user data
$user = $result->fetch_assoc();
// Verify the password
if (password_verify($inputPassword, $user['password'])) {
// Successful authentication
$_SESSION['userid'] = $user['id'];
$_SESSION['dispname'] = $user['dispname'];
echo json_encode(['success' => true, 'redirect' => 'main.php']);
} else {
// Authentication failed
echo json_encode(['success' => false, 'message' => 'Invalid username or password.']);
}
} else {
// User not found
echo json_encode(['success' => false, 'message' => 'Invalid username or password.']);
}
$stmt->close();
} else {
echo json_encode(['success' => false, 'message' => 'Failed to prepare the statement.']);
}
} else {
// Invalid request method
echo json_encode(['success' => false, 'message' => 'Invalid request method.']);
}
// Close the database connection
$conn->close();
?>