summaryrefslogtreecommitdiff
path: root/register.php
blob: cd72a37338d2095da9e49b5e221352e57089d774 (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
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
<!DOCTYPE html>
<html>
<head>
	<title>Register an account - cflip.net forum</title>
	<link rel="stylesheet" href="styles/style.css">
</head>
<body>
<?php include_once 'templates/header.php'?>
<h2>Register an account</h2>
<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>
<br>

<?php
include_once 'includes/db_inc.php';

function username_exists($dbc, $user_name) {
	$sql = "SELECT * FROM users WHERE user_name = ?;";
	$stmt = mysqli_stmt_init($dbc);

	if (!mysqli_stmt_prepare($stmt, $sql)) {
		die("Error: " . mysqli_error($dbc));
	}

	mysqli_stmt_bind_param($stmt, "s", $user_name);
	mysqli_stmt_execute($stmt);

	$result = mysqli_stmt_get_result($stmt);

	if ($row = mysqli_fetch_assoc($result)) {
		return $row;
	} else {
		$result = false;
		return $result;
	}

	mysqli_stmt_close($stmt);
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	$errors = array();

	$user_name = "";
	$user_pass = "";

	if (empty($_POST['user_name'])) {
		$errors[] = "You must provide a username.";
	} else {
		$user_name = $_POST['user_name'];
		if (!preg_match("/^[a-zA-Z0-9_]*$/", $user_name)) {
			$errors[] = "Username can only contain letters, numbers and underscores.";
		}

		if (strlen($user_name) > 30) {
			$errors[] = "Your username must be 30 characters or less.";
		}

		if (username_exists($dbc, $user_name) !== false) {
			$errors[] = "The username '" . $user_name . "' has already been taken by another user.";
		}
	}

	if (empty($_POST['user_pass'])) {
		$errors[] = "You must provide a password.";
	} else {
		$user_pass = $_POST['user_pass'];
		$pass_check = $_POST['user_pass_check'];

		if (preg_match("/^[a-zA-Z0-9\W]*$/", $user_pass) === false) {
			$errors[] = "Password contains invalid characters!";
		}

		if ($user_pass !== $pass_check) {
			$errors[] = "The two passwords do not match.";
		}
	}

	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(?, ?, NOW());";
		$stmt = mysqli_stmt_init($dbc);

		if (!mysqli_stmt_prepare($stmt, $sql)) {
			die('Could not create account due to internal error: ' . mysqli_error($dbc));
		}

		$pass_hash = password_hash($user_pass, PASSWORD_DEFAULT);

		mysqli_stmt_bind_param($stmt, "ss", $user_name, $pass_hash);
		mysqli_stmt_execute($stmt);
		mysqli_stmt_close($stmt);

		echo 'Account successfully registered! You can now <a href="signin.php">sign in</a>';
	}
}
?>
</body>
</html>