-
Notifications
You must be signed in to change notification settings - Fork 0
/
commisioner-signup.php
126 lines (106 loc) · 3.86 KB
/
commisioner-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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Commissioner Signup</title>
<link rel="stylesheet" href="grid.css" />
</head>
<body>
<?php
require('connect-db.php');
include('header.php');
// Check if a user exists
function user_check($user){
global $db;
$query = "SELECT * FROM admin WHERE admin_user = " . $db->quote($user);
$statement = $db->prepare($query);
$statement->execute();
$result = $statement->fetch();
$statement->closeCursor();
if (! $result){
return false;
} else {
return true;
}
}
// Create a user account with the given credentials. Return true if successful.
function create_account($user, $pwd, $name) {
global $db;
$query = "INSERT INTO admin (admin_user, admin_password, admin_name) VALUES (:user, :pwd, :name)";
$statement = $db->prepare($query);
$statement->bindValue(':user', $user);
$statement->bindValue(':pwd', $pwd);
$statement->bindValue(':name', $name);
$statement->execute();
return $statement->closeCursor();
}
function getAdminId($user) {
global $db;
$query = "SELECT admin_id FROM admin WHERE admin_user = " . $user;
$statement = $db->prepare($query);
$statement->execute();
$result = $statement->fetch();
$statement->closeCursor();
return $result;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && strlen($_POST['username']) > 0) {
$pwd = htmlspecialchars($_POST['pwd']);
$user = htmlspecialchars($_POST['username']);
$name = htmlspecialchars($_POST['name']);
if (user_check($user)){
echo "Error: unable to create account. Try a new username.";
} elseif (create_account($user, $pwd, $name)) {
$_SESSION['user'] = $user;
$_SESSION['id'] = getAdminId($user);
$_SESSION['admin'] = true;
setcookie('name', $name, time() + 3600);
header('Location: success.php');
} else {
echo "Error - unable to create account, external error.";
}
}
?>
<div class="grid-container">
<div class="grid-header">
<h1>Create an account</h1>
</div>
<div class="grid-row">
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onsubmit="return validateInfo()">
<h3>Name: </h3>
<input type="text" name="name" class="grid-input" autofocus required />
<br/>
<h3>Username: </h3>
<input type="text" name="username" id="username" class="grid-input" required />
<div id="user-msg" class="feedback"></div>
<br/>
<h3>Password: </h3>
<input type="password" name="pwd" class="grid-input" required />
<div id="pwd-msg" class="feedback"></div>
<br/>
<input type="submit" class="btn-grid" value="Sign up" /> <!-- use input type="submit" with the required attribute -->
</form>
</div>
</div>
<script>
var user = document.getElementById("username");
user.addEventListener('blur', function() {
checkUsername(8);
}, false);
function checkUsername(minlength) {
var msg = document.getElementById("user-msg");
if (user.value.length < minlength && user.value.length > 0) {
msg.textContent = "Username is too short";
} else {
msg.textContent = "";
}
}
function validateInfo() {
if (user.value.length < 8)
{
alert("Username must be at least 8 characters in length.")
return false;
}
return true;
}
</script>
</body>
</html>