blob: f0cb3f5817e6f0051717f4ee8455faf95f876ee0 (
plain)
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
|
<?php
include_once 'header.php';
include_once 'includes/db_inc.php';
echo '<section><h2>Register an account</h2>';
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
echo '
<form action="register.php" method="post">
<label for="user_name">Username: </label><br>
<input type="text" name="user_name"><br>
<label for="user_pass">Password: </label><br>
<input type="password" name="user_pass"><br>
<label for="user_pass_check">Re-enter password: </label><br>
<input type="password" name="user_pass_check"><br>
<input type="submit" name="submit">
</form>
';
} else {
$errors = array();
if (isset($_POST['user_name'])) {
if (!ctype_alnum($_POST['user_name'])) {
$errors[] = 'Invalid username. Only letters and numbers are supported.';
}
if (strlen($_POST['user_name']) > 30) {
$errors[] = 'Username must be 30 characters or less.';
}
} else {
$errors[] = 'Please provide a username.';
}
if (isset($_POST['user_pass'])) {
if ($_POST['user_pass'] != $_POST['user_pass_check']) {
$errors[] = 'The two passwords do not match.';
}
} else {
$errors[] = 'Please provide a password.';
}
if (!empty($errors)) {
echo 'Please check the following problems: <ul>';
foreach ($errors as $err) {
echo '<li>' . $err . '</li>';
}
echo '</ul>';
} else {
$sql = "INSERT INTO users(user_name, user_pass, user_date)
VALUES('" . mysqli_real_escape_string($dbc, $_POST['user_name']) . "',
'" . sha1($_POST['user_pass']) . "',
NOW())
";
$result = mysqli_query($dbc, $sql);
if (!$result) {
echo 'Failed to register account due to internal error.';
echo mysqli_error($dbc);
} else {
echo 'Account successfully created!';
}
}
}
echo '</section>';
include_once 'footer.php';
|