-
Notifications
You must be signed in to change notification settings - Fork 0
/
registerCheck.php
92 lines (91 loc) · 2.2 KB
/
registerCheck.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
<!DOCTYPE html>
<head>
<meta charset="utf-8"/>
<title>RegisterCheck</title>
<style type="text/css">
body{
width: 760px; /* how wide to make your web page */
background-color: teal; /* what color to make the background */
margin: 0 auto;
padding: 0;
font:12px/16px Verdana, sans-serif; /* default font */
}
div#main{
background-color: #FFF;
margin: 0;
padding: 10px;
}
</style>
</head>
<body><div id="main">
<?php
require 'database.php';
$username = $_POST['username'];
$password = $_POST['password'];
$repeatPassword = $_POST['repeatPassword'];
if(!preg_match('/^[\w_\.\-]+$/', $username) ){
echo "Invalid username, username should only be consisted by letters and numbers.";
?>
<p>
<form name="register" action="register.html" method="POST">
<input type="submit" value="Back to register page" />
</form>
</p>
<?php
exit;
}
$stmt = $mysqli->prepare("select username from users");
if(!$stmt){
printf("Query Prep Failed: %s\n", $mysqli->error);
exit;
}
$stmt->execute();
$stmt->bind_result($databaseUsername);
while($stmt->fetch()){
$compare = strcmp($username, htmlspecialchars($databaseUsername));
if ($compare == 0) {
echo "Username exists already, please try another username.";
?>
<p>
<form name="register" action="register.html" method="POST">
<input type="submit" value="Back to register page" />
</form>
</p>
<?php
exit;
}
}
$stmt->close();
$compare = strcmp($password, $repeatPassword);
if ($compare == 0) {
$stmt = $mysqli->prepare("insert into users (username, salted_hash_pass) values (?, ?)");
if(!$stmt){
printf("Query Prep Failed: %s\n", $mysqli->error);
exit;
}
$stmt->bind_param('ss', $username, password_hash($password, PASSWORD_BCRYPT));
$stmt->execute();
$stmt->close();
echo "Successfully registered!";
?>
<p>
<form name="homepage" action="homepage.php" method="POST">
<input type="submit" value="Back to homepage" />
</form>
</p>
<?php
exit;
} else {
echo "Your passwords input do not match.";
?>
<p>
<form name="register" action="register.html" method="POST">
<input type="submit" value="Back to register page" />
</form>
</p>
<?php
exit;
}
?>
</div></body>
</html>