-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup.php
More file actions
179 lines (137 loc) · 6.69 KB
/
signup.php
File metadata and controls
179 lines (137 loc) · 6.69 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
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
<?php require_once("database/config.php");
if (isset($_SESSION['login_sess'])) {
if ($_SESSION['login_sess'] == "1") {
$_SESSION['email'] = $email;
header('Location:home_page.php');
} else {
echo "0";
}
}
// Handle the form submission
if (isset($_POST['signup'])) {
extract($_POST);
// Validation logic
if (strlen($email) > 50) {
$error[] = 'Email: max length is 50 characters';
}
if ($passwordConfirm == '') {
$error[] = 'Please confirm the password';
}
if ($password != $passwordConfirm) {
$error[] = 'Passwords do not match';
}
if (strlen($password) < 6) {
$error[] = 'Password should be at least 6 characters long';
}
if (strlen($password) > 20) {
$error[] = 'Password: Max length 20 characters not allowed';
}
// Check if email already exists in the database
$sql = "SELECT * FROM users WHERE email='$email'";
$res = mysqli_query($dbc, $sql);
if (mysqli_num_rows($res) > 0) {
$row = mysqli_fetch_assoc($res);
if ($email == $row['email']) {
$error[] = 'Email already exists';
}
}
// If no errors, proceed with registration
if (!isset($error)) {
$options = array("cost" => 4);
$password = password_hash($password, PASSWORD_BCRYPT, $options);
$result = mysqli_query($dbc, "INSERT INTO users VALUES('', '$name', '$email', '$password')");
if ($result) {
$done = 2;
} else {
$error[] = 'Failed: Something went wrong';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Signup Page</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
<?php if (isset($error)): ?>
<div class="col">
<?php foreach ($error as $error): ?>
<div class="alert alert-danger" role="alert">⚠
<?php echo $error; ?>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if (isset($done)): ?>
<?php header("location:login.php"); ?>
<?php else: ?>
<h2 class="mb-4 text-center ">Sign-up</h2>
<form method="post" class="row g-3">
<!-- Name Input -->
<div class="col-md-6">
<label for="name" class="form-label">Name*</label>
<input type="text" name="name" class="form-control" placeholder="Enter Your Name"
value="<?php if (isset($error)) echo $name; ?>" required>
</div>
<!-- Email Input -->
<div class="col-md-6">
<label for="email" class="form-label">Email*</label>
<input type="email" name="email" class="form-control"
placeholder="Enter Your Email Address" value="<?php if (isset($error)) echo $email; ?>" required>
</div>
<!-- Password Input -->
<div class="col-md-6">
<label for="password" class="form-label">Password*</label>
<input type="password" name="password" id="password" class="form-control"
placeholder="Enter Your Password" required>
<div class="form-check mt-2">
<input class="form-check-input" type="checkbox" id="showPassword">
<label class="form-check-label" for="showPassword">Show password</label>
</div>
</div>
<!-- Confirm Password Input -->
<div class="col-md-6">
<label for="passwordConfirm" class="form-label">Confirm Password*</label>
<input type="password" name="passwordConfirm" id="passwordConfirm"
class="form-control" placeholder="Confirm Your Password" required>
<div class="form-check mt-2">
<input class="form-check-input" type="checkbox" id="showPasswordConfirm">
<label class="form-check-label" for="showPasswordConfirm">Show
password</label>
</div>
</div>
<!-- Sign-up Button -->
<div class="col-12">
<button type="submit" name="signup" class="btn btn-primary">Sign-up</button>
</div>
<!-- Login Link -->
<div class="col-12">
<p>Have an Account? <a href="login.php">Login</a></p>
<p>Signup as a owner <a href="owner_signup.php">Owner Signup</a></p>
</div>
</form>
<?php endif; ?>
</div>
<!-- Bootstrap JS and JS for password visibility toggle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<script>
const passwordInput = document.getElementById('password');
const passwordConfirmInput = document.getElementById('passwordConfirm');
const showPasswordCheckbox = document.getElementById('showPassword');
const showPasswordConfirmCheckbox = document.getElementById('showPasswordConfirm');
showPasswordCheckbox.addEventListener('change', function () {
passwordInput.type = showPasswordCheckbox.checked ? 'text' : 'password';
});
showPasswordConfirmCheckbox.addEventListener('change', function () {
passwordConfirmInput.type = showPasswordConfirmCheckbox.checked ? 'text' : 'password';
});
</script>
</body>
</html>