-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup.php
More file actions
147 lines (133 loc) · 5.04 KB
/
signup.php
File metadata and controls
147 lines (133 loc) · 5.04 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
require 'config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$confirm_password = $_POST['confirm-password'] ?? '';
if (empty($name) || empty($email) || empty($password) || empty($confirm_password)) {
echo "<script>
alert('All fields are required.');
window.location.href = 'signup.php';
</script>";
exit;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<script>
alert('Invalid email format.');
window.location.href = 'signup.php';
</script>";
exit;
}
if ($password !== $confirm_password) {
echo "<script>
alert('Passwords do not match.');
window.location.href = 'signup.php';
</script>";
exit;
}
if (strlen($password) < 6) {
echo "<script>
alert('Password must be at least 6 characters long.');
window.location.href = 'signup.php';
</script>";
exit;
}
$password = password_hash($password, PASSWORD_DEFAULT);
// Check if connection is valid
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if user already exists
$stmt = $conn->prepare("SELECT email FROM USERS WHERE email = ?");
if (!$stmt) {
echo "<script>
alert('Database error: " . $conn->error . "');
window.location.href = 'signup.php';
</script>";
exit;
}
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
echo "<script>
alert('User already exists! Please login.');
window.location.href = 'login.php';
</script>";
$stmt->close();
exit;
} else {
$stmt->close();
// Insert new user
$insert_stmt = $conn->prepare("INSERT INTO USERS(name, email, password) VALUES(?, ?, ?)");
if (!$insert_stmt) {
echo "<script>
alert('Database error: " . $conn->error . "');
window.location.href = 'signup.php';
</script>";
exit;
}
$insert_stmt->bind_param("sss", $name, $email, $password);
if ($insert_stmt->execute()) {
$insert_stmt->close();
echo "<script>
alert('Account created successfully! Please login.');
window.location.href = 'login.php';
</script>";
exit;
} else {
// Error
echo "<script>
alert('Error creating account: " . $insert_stmt->error . "');
window.location.href = 'signup.php';
</script>";
$insert_stmt->close();
exit;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="./login.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Signup</title>
</head>
<body>
<div class="form-container">
<div style="display: flex; flex-direction: row-reverse; justify-content: space-between; align-items: center;">
<h1><Code<span style="color: #1a4eaf">Case></span></h1>
<h1><span style="text-align: left;">Create an account</span></h1>
</div>
<form action="signup.php" method="POST">
<div class="form-row">
<div class="form-group">
<label for="firstName">Enter Name</label>
<input required type="text" id="firstName" placeholder="Name" name="name">
</div>
<div class="form-group">
<label for="email">Enter Email</label>
<input required type="email" id="email" placeholder="Email" name="email">
</div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input required type="password" id="password" placeholder="Password" name="password">
</div>
<div class="form-group">
<label for="confirm-password">Confirm Password</label>
<input required type="password" id="confirm-password" placeholder="Re-enter Password" name="confirm-password">
</div>
<div class="checkbox-container">
<input required type="checkbox" id="terms" name="terms">
<label for="terms" class="checkbox-label">I accept the <a href="#">Terms and Conditions</a></label>
</div>
<button type="submit">Create an account</button>
<div style="display: flex; justify-content: center; margin-top: 10px; gap: 5px;">Already have an account? <a href="./login.php">Login</a></div>
</form>
</div>
</body>
</html>