diff options
author | Cflip <36554078+cflip@users.noreply.github.com> | 2021-01-22 20:45:43 -0700 |
---|---|---|
committer | Cflip <36554078+cflip@users.noreply.github.com> | 2021-01-22 20:45:43 -0700 |
commit | df49a36e140acc211fdc31480d40281404110310 (patch) | |
tree | 3a25af561ed3703ad5df8cf90d9f56d8824f7a4f /register.php |
Inital commit with existing code
Diffstat (limited to 'register.php')
-rw-r--r-- | register.php | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/register.php b/register.php new file mode 100644 index 0000000..8f74aa9 --- /dev/null +++ b/register.php @@ -0,0 +1,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>
\ No newline at end of file |