-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.php
140 lines (118 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
<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
unset($_SESSION['username']);
require_once('connection_pdo.php');
// Check if an admin is logged in
if (!isset($_SESSION['username_admin'])) {
// Optional: You can comment out the following line if you want regular users to access this page.
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>Register</title>
<link rel="stylesheet" href="css/style1.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="container">
<div class="form-box box">
<header>Sign Up</header>
<hr>
<form action="#" method="POST">
<?php
if (isset($_POST['register'])) {
$name = $_POST['username'];
$pass = $_POST['password'];
$cpass = $_POST['cpass'];
$isAdmin = isset($_POST['isAdmin']) ? 1 : 0; // Get checkbox value
// Check if username is already registered
$checkStmt = $connection_pdo->prepare("SELECT * FROM users WHERE username = :name");
$checkStmt->bindParam(':name', $name);
$checkStmt->execute();
if ($checkStmt->rowCount() > 0) {
echo "<div class='message'><p>This Username is already in use. Please choose another one.</p></div><br>";
echo "<a href='javascript:self.history.back()'><button class='btn'>Go Back</button></a>";
} else {
// Hash the password
$passwd = password_hash($pass, PASSWORD_DEFAULT);
// Insert user into database if passwords match
if ($pass === $cpass) {
// Insert into users table first
$insertStmt = $connection_pdo->prepare("INSERT INTO users(username, password) VALUES (:name, :passwd)");
$insertStmt->bindParam(':name', $name);
$insertStmt->bindParam(':passwd', $passwd);
$result = $insertStmt->execute();
// If the user is an admin, insert into users_admin as well
if ($result && $isAdmin) {
$adminInsertStmt = $connection_pdo->prepare("INSERT INTO users_admin(username, password) VALUES (:name, :passwd)");
$adminInsertStmt->bindParam(':name', $name);
$adminInsertStmt->bindParam(':passwd', $passwd);
$adminInsertStmt->execute(); // No need to check the result for admin insertion
}
if ($result) {
echo "<div class='message'><p>You have registered successfully!</p></div><br>";
echo "<a href='login.php'><button class='btn'>Login Now</button></a>";
} else {
echo "<div class='message'><p>Registration failed. Please try again later.</p></div><br>";
echo "<a href='signup.php'><button class='btn'>Go Back</button></a>";
}
} else {
echo "<div class='message'><p>Passwords do not match.</p></div><br>";
echo "<a href='signup.php'><button class='btn'>Go Back</button></a>";
}
}
} else {
?>
<div class="input-container">
<i class="fa fa-user icon"></i>
<input class="input-field" type="text" placeholder="Username" name="username" required>
</div>
<div class="input-container">
<i class="fa fa-lock icon"></i>
<input class="input-field password" type="password" placeholder="Password" name="password" required>
<i class="fa fa-eye icon toggle"></i>
</div>
<div class="input-container">
<i class="fa fa-lock icon"></i>
<input class="input-field" type="password" placeholder="Confirm Password" name="cpass" required>
</div>
<!-- Admin tickbox with spacing -->
<div class="input-container">
<input type="checkbox" id="isAdmin" name="isAdmin">
<label for="isAdmin" style="margin-left: 10px;">Register as Admin</label> <!-- Add margin for spacing -->
</div>
<center>
<input type="submit" name="register" id="submit" value="Signup" class="btn">
</center>
<?php
}
?>
</form>
<center>
<a href="admin_home.php"><button class="btn" style="margin-top: 10px;">Home</button></a> <!-- Home button outside the form -->
</center>
</div>
</div>
<script>
const toggle = document.querySelector(".toggle"),
input = document.querySelector(".password");
toggle.addEventListener("click", () => {
if (input.type === "password") {
input.type = "text";
toggle.classList.replace("fa-eye", "fa-eye-slash");
} else {
input.type = "password";
toggle.classList.replace("fa-eye-slash", "fa-eye");
}
})
</script>
</body>
</html>