summaryrefslogtreecommitdiff
path: root/register.php
blob: 8f74aa9264be180e9ecc9b7f9c6015882aad5de8 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html>
<head>
	<title>cflip.net forum</title>
	<link rel="stylesheet" href="style.css">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="wrapper">
	<h1>Register an account</h1>
	<nav>
		<a href="index.php">Go back</a> Already have an account? <a href="signin.php">Sign in</a>
	</nav>
	<section>
<?php

include_once 'connect.php';

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!';
		}
	}
}

?>
	</section>
	<footer>Copyright © 2021 cflip.net</footer>
</div>
</body>
</html>