-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup_process.php
More file actions
63 lines (55 loc) · 1.93 KB
/
Copy pathsignup_process.php
File metadata and controls
63 lines (55 loc) · 1.93 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
<?php
session_start();
require 'db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$fullname = trim($_POST['fullname'] ?? '');
$email = trim($_POST['email'] ?? '');
$password = trim($_POST['password'] ?? '');
// Validate
if (empty($fullname) || empty($email) || empty($password)) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'All fields required']);
exit();
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Invalid email']);
exit();
}
if (strlen($password) < 6) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Password must be 6+ characters']);
exit();
}
// Check if email exists
$check = $conn->prepare("SELECT id FROM users WHERE email = ?");
$check->bind_param("s", $email);
$check->execute();
if ($check->get_result()->num_rows > 0) {
http_response_code(409);
echo json_encode(['success' => false, 'message' => 'Email already exists']);
exit();
}
// Insert user
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = $conn->prepare("INSERT INTO users (fullname, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $fullname, $email, $hashed_password);
if ($stmt->execute()) {
$_SESSION['user_id'] = $conn->insert_id;
$_SESSION['user'] = $fullname;
$_SESSION['email'] = $email;
echo json_encode([
'success' => true,
'message' => 'Signup successful',
'user' => [
'fullname' => $fullname,
'email' => $email,
'role' => 'user'
]
]);
} else {
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Signup failed']);
}
}
?>