generated from GenerateNU/GenerateHWBase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userNameCheck.php
65 lines (55 loc) · 2.01 KB
/
userNameCheck.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
60
61
62
63
64
65
<?php
// Database connection parameters
$host = "localhost";
$username = "root";
$password = "usbw";
$database = "test";
// Establishing connection to the database
$mysqli = new mysqli($host, $username, $password, $database);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
// Check if the username is provided in the POST request
if(isset($_POST['username'])) {
$username = $_POST['username'];
// Prepare a SQL statement to check if the username exists in the database
$sql = "SELECT userName FROM data WHERE userName = ?";
// Using prepared statement to prevent SQL injection
if ($stmt = $mysqli->prepare($sql)) {
// Binding parameters
$stmt->bind_param("s", $username);
// Executing the statement
if ($stmt->execute()) {
// Fetching the result
$result = $stmt->get_result();
// Checking if any rows are returned (username exists)
if ($result->num_rows > 0) {
// Redirect to user.php
if ($username == "admin") {
header("Location: admin.php");
exit;
}
else {
header("Location: user.php?username=" . $username);
exit;
}
} else {
echo "Username not found. Please try again."; // Username doesn't exist
// Redirect back to the login page
header("Location: index.html");
exit;
}
} else {
echo "Error executing query: " . $stmt->error;
}
// Closing the statement
$stmt->close();
} else {
echo "Error preparing statement: " . $mysqli->error;
}
} else {
echo "Username not provided";
}
// Closing the database connection
$mysqli->close();
?>