-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_signup.php
67 lines (51 loc) · 1.86 KB
/
process_signup.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
66
67
<?php
require_once 'db_connection.php';
// Check if the form was submitted via POST
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate and sanitize form inputs
$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
// Validate inputs (example checks, adjust as per your requirements)
if (empty($name) || empty($email) || empty($password)) {
die("All fields are required");
}
// Validate email format
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
die("Invalid email format");
}
// Check if email already exists
$check_email_sql = "SELECT id FROM users WHERE email = ?";
$check_email_stmt = $conn->prepare($check_email_sql);
if ($check_email_stmt === false) {
die("SQL prepare error: " . $conn->error);
}
$check_email_stmt->bind_param("s", $email);
$check_email_stmt->execute();
$check_email_stmt->store_result();
if ($check_email_stmt->num_rows > 0) {
die("Email address already taken");
}
$check_email_stmt->close();
// Hash the password
$password_hash = password_hash($password, PASSWORD_DEFAULT);
// Insert into database
$sql = "INSERT INTO users (name, email, password_hash) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sql);
if ($stmt === false) {
die("SQL prepare error: " . $conn->error);
}
$stmt->bind_param("sss", $name, $email, $password_hash);
if ($stmt->execute()) {
// Success: Send response back to JavaScript indicating success
echo "User registered successfully";
} else {
echo "Error: " . $conn->error;
}
$stmt->close();
} else {
// Handle cases where the script is accessed directly without POST data
die("Invalid request");
}
$conn->close(); // Close the database connection
?>