forked from tasminoni/CSE370-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.php
196 lines (166 loc) · 5.92 KB
/
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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
$errors = array();
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// Check if user type is selected
if (empty($_POST["name"])) {
$errors['name'] = "Name is required";
}
if (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
$errors['email'] = "Valid email is required";
}
if (strlen($_POST["password"]) < 8) {
$errors['password'] = "Password must be at least 8 characters";
} elseif (!preg_match("/[a-z]/i", $_POST["password"])) {
$errors['password'] = "Password must contain at least one letter";
} elseif (!preg_match("/[0-9]/", $_POST["password"])) {
$errors['password'] = "Password must contain at least one number";
}
if ($_POST["password"] !== $_POST["password_confirmation"]) {
$errors['password_confirmation'] = "Passwords must match";
}
if (empty($errors)) {
$servername = "localhost"; // Replace with your MySQL server address
$username = "root"; // Replace with your MySQL username
$password = ""; // Replace with your MySQL password
$database = "db"; // Replace with your MySQL database name
// Create connection
$mysqli = new mysqli($servername, $username, $password, $database);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Check if email already exists
$check_sql = "SELECT * FROM user WHERE email = ?";
$check_stmt = $mysqli->prepare($check_sql);
$check_stmt->bind_param("s", $_POST["email"]);
$check_stmt->execute();
$result = $check_stmt->get_result();
if ($result->num_rows > 0) {
$errors['email'] = "Email already taken";
$check_stmt->close();
$mysqli->close();
} else {
$check_stmt->close();
// Insert user data into the database
$sql = "INSERT INTO user (name, email, password_hash) VALUES (?, ?, ?)";
$stmt = $mysqli->prepare($sql);
if (!$stmt) {
die("SQL error: " . $mysqli->error);
}
$password_hash = password_hash($_POST["password"], PASSWORD_DEFAULT);
$stmt->bind_param("sss", $_POST["name"], $_POST["email"], $password_hash);
$stmt->execute();
$stmt->close();
$mysqli->close();
header("Location: login.php");
exit;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Signup</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Roboto', sans-serif;
background-color: #f9f9f9;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
form {
background-color: #fff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
width: 300px;
}
h1 {
text-align: center;
color: #333;
}
label {
display: block;
margin-bottom: 6px;
color: #555;
}
input[type="text"],
input[type="email"],
input[type="password"] {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
select {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.error {
color: red;
font-size: 14px;
margin-top: -10px;
margin-bottom: 10px;
}
button {
width: 100%;
padding: 12px;
border: none;
border-radius: 4px;
background-color: #007bff;
color: #fff;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #0056b3;
}
body {
background-image: url('images/img.jpeg');
background-size: cover;
background-repeat: no-repeat;
}
</style>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" id="signup" novalidate>
<h1>Signup</h1>
<div>
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''; ?>" required>
<?php if(isset($errors['name'])) echo '<div class="error">' . $errors['name'] . '</div>'; ?>
</div>
<div>
<label for="email">Email</label>
<input type="email" id="email" name="email" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''; ?>" required>
<?php if(isset($errors['email'])) echo '<div class="error">' . $errors['email'] . '</div>'; ?>
</div>
<div>
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<?php if(isset($errors['password'])) echo '<div class="error">' . $errors['password'] . '</div>'; ?>
</div>
<div>
<label for="password_confirmation">Confirm Password</label>
<input type="password" id="password_confirmation" name="password_confirmation" required>
<?php if(isset($errors['password_confirmation'])) echo '<div class="error">' . $errors['password_confirmation'] . '</div>'; ?>
</div>
<button type="submit">Sign up</button>
</form>
</body>
</html>