-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup.php
More file actions
66 lines (52 loc) · 1.96 KB
/
signup.php
File metadata and controls
66 lines (52 loc) · 1.96 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
<?php
// Database connection details
$host = 'localhost';
$db = 'coursehub'; // Replace with your database name
$user = 'root'; // Replace with your database username
$pass = ''; // Replace with your database password
// Create a connection
$conn = new mysqli($host, $user, $pass, $db);
// Check connection
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Retrieve and sanitize form inputs
$username = htmlspecialchars(trim($_POST['username']));
$email = htmlspecialchars(trim($_POST['email']));
$password = htmlspecialchars(trim($_POST['password']));
$confirm_password = htmlspecialchars(trim($_POST['confirm_password']));
// Validate form inputs
if (empty($username) || empty($email) || empty($password) || empty($confirm_password)) {
die('All fields are required.');
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
die('Invalid email address.');
}
if ($password !== $confirm_password) {
die('Passwords do not match.');
}
// Hash the password for security
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
// Check if the username or email already exists
$stmt = $conn->prepare('SELECT * FROM users WHERE username = ? OR email = ?');
$stmt->bind_param('ss', $username, $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
die('Username or email is already taken.');
}
$stmt->close();
// Insert the new user into the database
$stmt = $conn->prepare('INSERT INTO users (username, email, password) VALUES (?, ?, ?)');
$stmt->bind_param('sss', $username, $email, $hashed_password);
if ($stmt->execute()) {
header('Location: login.html');
exit();
} else {
echo 'Error: ' . $stmt->error;
}
$stmt->close();
}
$conn->close();
?>