-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.php
187 lines (170 loc) · 7.03 KB
/
register.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
<?php
session_start();
require_once('common.php');
//if (isset($username)){
// header('Location:main.php');
//unset($_SESSION["newsession"]);
//}
$db = connectToDB();
$username="";
$fullname="";
$error_message = "";
/**** LOGIN LOGIC *******/
if(isset($_POST['submit'])) {
//validate data
$username = clean_input($_POST['username']);
$fullname = clean_input($_POST['fullname']);
$password = $_POST["password"];
$authcode = $_POST["authcode"];
if(strlen($username) < 5) $error_message = "Username must be at least 5 characters";
if(strlen($password) < 6) $error_message = "Password must be at least 6 characters";
if(strlen($authcode) != 5) $error_message = "Authorization code must be 5 digits";
if($authcode != AUTHCODE) $error_message = "Incorrect authorization code";
$pwdhash = password_hash($password, PASSWORD_DEFAULT);
$password = "---";
//create and perfrom SQL
$sql = "SELECT username FROM users WHERE username = ?";
if ($stmt = $db->prepare($sql)) {
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
$stmt->fetch();
$stmt->close();
} else {
$message_ = 'Invalid query: ' . mysqli_error($db) . "\n<br>";
$message_ .= 'SQL: ' . $sql;
die($message_);
}
$row_cnt = mysqli_num_rows($result);
if ($row_cnt > 0) {
$error_message = "** That user already exists! ** <a href=\"index.php\"><div class=\"btn btn-secondary float-right shadow\">Go to Login</div></a>";
}
// error message ...
if ($error_message != "") $error_message = '<div class="card"><div class="alert text-white bg-danger"><b>'. $error_message .'</b></div></div>';
if (empty($error_message)) {
#$sql = "INSERT INTO users(username, fullname, password) VALUES('$username','$fullname','$pwdhash')";
$sql = "INSERT INTO users(username, fullname, password) VALUES(?, ?, ?)";
if ($stmt = $db->prepare($sql)) {
$stmt->bind_param("sss", $username, $fullname, $pwdhash);
$stmt->execute();
$result = $stmt->get_result();
$stmt->fetch();
$stmt->close();
} else {
$message_ = 'Invalid query: ' . mysqli_error($db) . "\n<br>";
$message_ .= 'SQL: ' . $sql;
die($message_);
}
//sudo chown www-data: files
mkdir("files/$username");
//set session variables
$_SESSION["username"] = $username;
$_SESSION["fullname"] = $fullname;
#$_SESSION["pwdhash"] = $pwdhash;
//This is set here upon login (AND ALSO IN register.php) and then session-authkey is never set again.
$_SESSION["authkey"] = AUTHKEY;
header('Location:main.php');
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>File Uploader - register</title>
<link rel="stylesheet" href="./resources/bootstrap.min.css">
<!-- <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>-->
<!-- <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>-->
<!-- <meta name="viewport" content="width=device-width, initial-scale=1.0">-->
</head>
<body>
<!-- This form will call either login.php or register.php with the same fields. -->
<script>
//This makes sure that the data fields have been filled in before the form is submitted. It uses the ID error_message .
function validateData() {
var x, text;
x = document.getElementById("username").value;
if (!x || 0 === x.length) {
text = "You must include a username";
document.getElementById("error_message").outerHTML =
'<div id="error_message" class="alert alert-danger w-50"></div>';
document.getElementById("username").outerHTML =
'<input type="text" name="username" id="username" class="form-control border-danger" placeholder="Username">';
document.getElementById("error_message").innerHTML = text;
document.getElementById("username").value = "";
return false;
}
x = document.getElementById("fullname").value;
if (!x || 0 === x.length) {
text = "You must include your full name";
document.getElementById("error_message").outerHTML =
'<div id="error_message" class="alert alert-danger w-50"></div>';
document.getElementById("fullname").outerHTML =
'<input type="text" name="fullname" id="fullname" class="form-control border-danger" placeholder="Fullname">';
document.getElementById("error_message").innerHTML = text;
document.getElementById("fullname").value = "";
return false;
}
x = document.getElementById("password").value;
if (!x || 0 === x.length) {
text = "You must include a password";
document.getElementById("error_message").outerHTML =
'<div id="error_message" class="alert alert-danger w-50 "></div>';
document.getElementById("password").outerHTML =
'<input type="text" name="password" id="password" class="form-control border-danger" placeholder="Password">';
document.getElementById("error_message").innerHTML = text;
document.getElementById("password").value = "";
return false;
}
// document.getElementById("form1").action = "/lockers/edit/" + x;
return true;
}
</script>
<a href="help.html"><button class="btn m-1 btn-outline-primary ">Help</button></a>
<div class="container mt-5 w-50 text-center">
<h2>Welcome to the <b>File Upload Centre</b></h2>
<div id="error_message"></div>
<div class="card">
<div class="card-header bg-info text-white"><b>For ICS class to upload java files</b></div>
<div class="card-body">
<p class="">Register and then start your session</p>
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post" onsubmit="return validateData()">
<div class="input-group mb-3">
<input type="text" name="username" id="username" class="form-control" placeholder="Username"
<?php if (!empty($username)) echo "value=$username";?>>
<div class="input-group-append">
<div class="input-group-text"> <span class="fa fa-envelope"></span> </div>
</div>
</div>
<div class="input-group mb-3">
<input type="text" name="fullname" id="fullname" class="form-control" placeholder="Full name"
<?php if (!empty($fullname)) echo "value=$fullname";?>>
<div class="input-group-append">
<div class="input-group-text"> <span class="fa fa-user"></span> </div>
</div>
</div>
<div class="input-group mb-3">
<input type="password" name="password" id="password" class="form-control"
placeholder="Password">
<div class="input-group-append">
<div class="input-group-text"> <span class="fa fa-lock"></span> </div>
</div>
</div>
<div class="row">
<div class="col-8 text-left">
<label for="authcode" class="" data-toggle="tooltip" data-placement="top" title="get it from your teacher">Enter authorization code</label>
<input type="text" class="" id="authcode" name = "authcode" size="5" placeholder="00000"><br>
<i class="text-secondary">Get it from your teacher</i>
</div>
<div class="col-4">
<button type="submit" name="submit" class="btn btn-primary btn-block">Register</button>
</div>
<!-- /.col -->
</div>
</form>
</div>
</div>
<?php if ($error_message != "") echo $error_message; ?>
</div>
</body>
</html>