Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $error = false;
- $message = $errorUsername = $errorEmail = $errorPassword = "";
- if (isset($_POST['submit'])) {
- // Check username criteria
- if (empty($_POST['username'])) {
- $errorUsername .= "No username written! ";
- $error = true;
- } else {
- if (!preg_match('/^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/', $_POST['username'])) {
- $errorUsername .= "Not a valid username! ";
- $error = true;
- } else {
- $username = htmlentities($_POST['username']);
- // Checks if username is atleast 3 characters
- if (strlen($username) < 3) {
- $errorUsername .= "The username is too short. Minimum 3 characters! ";
- $error = true;
- // Checks if username is too long
- } else if (strlen($username) > 32) {
- $errorUsername .= "The username is too long. Maximum 32 characters! ";
- $error = true;
- } else {
- try {
- $stmt = $db->prepare("SELECT username FROM users WHERE username=:username");
- $stmt->bindParam(":username", $username, PDO::PARAM_STR);
- $stmt->execute();
- // Checks if username exists in the database
- if ($stmt->rowCount() > 0) {
- $errorUsername .= "That username is already taken! ";
- $error = true;
- }
- } catch (PDOException $e) {
- echo $e->getMessage() . PHP_EOL;
- $error = true;
- }
- }
- }
- }
- // Checks if email is typed
- if (empty($_POST['email'])) {
- $errorEmail .= "No email written! ";
- $error = true;
- } else {
- $email = htmlentities($_POST['email']);
- // Checks if email is a valid email
- if (!(filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/@.+\./', $email))) {
- $errorEmail .= "Not a valid email address! ";
- $error = true;
- } else {
- try {
- $stmt = $db->prepare("SELECT email FROM users WHERE email=:email");
- $stmt->bindParam(":email", $email, PDO::PARAM_STR);
- $stmt->execute();
- // Checks if email exists in database
- if ($stmt->rowCount() > 0) {
- $errorEmail .= "That email address is already in use! ";
- $error = true;
- }
- } catch (PDOException $e) {
- echo $e->getMessage();
- $error = true;
- }
- }
- }
- // Checks if password is typed
- if (empty($_POST['password'])) {
- $errorPassword .= "No password written! ";
- $error = true;
- } else {
- // Checks if second password is typed
- if (empty($_POST['passwordConfirm'])) {
- $errorPassword .= "No confirmed password written! ";
- $error = true;
- }
- // Checks if passwords match
- if ($_POST['password'] != $_POST['passwordConfirm']) {
- $errorPassword .= "Passwords does not match! ";
- $error = true;
- // Checks if password is atleast 8 characters
- } else if (strlen($_POST['password']) <= 8) {
- $errorPassword .= "Password is too short. Minimum 8 characters! ";
- $error = true;
- }
- }
- // Now we can create user and do the query!
- if (!$error) {
- // Hashes the password
- $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
- // Create date (1992-05-14)
- $date = date("Y-m-d");
- echo $username . "<br />";
- echo $email . "<br />";
- echo $password . "<br />";
- echo $date . "<br />";
- try {
- $stmt = $db->prepare("INSERT INTO users (username, email, password, created) VALUES (:username, :email, :password, :created)");
- $stmt->bindParam(":username", $username, PDO::PARAM_STR);
- $stmt->bindParam(":email", $email, PDO::PARAM_STR);
- $stmt->bindParam(":password", $password, PDO::PARAM_STR);
- $stmt->bindParam(":created", $date, PDO::PARAM_STR);
- $stmt->execute();
- $message .= "User is made!";
- } catch (PDOException $e) {
- echo $e->getMessage() . PHP_EOL;
- }
- }
- }
- ?>
- <ul>
- <?php if (!$_SESSION['user']) { ?>
- <li><a href="index.php">Home</a></li>
- <li><a href="login.php">Login</a></li>
- <?php } else { ?>
- <li><a href="index.php">Home</a></li>
- <li><a href="logout.php">Logout</a></li>
- <?php } ?>
- </ul>
- <form action="register.php" method="post" name="register">
- <table>
- <tr>
- <td>Username</td>
- <td>
- <input type="text" name="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>">
- </td>
- <td style="color:red;">
- <?php if (!empty($errorUsername)) { echo $errorUsername; } ?>
- </td>
- </tr>
- <tr>
- <td>Email</td>
- <td>
- <input type="email" name="email" value="<?php if (isset($_POST['email'])) echo htmlentities($_POST['email']); ?>">
- </td>
- <td style="color:red;">
- <?php if (!empty($errorEmail)) { echo $errorEmail; } ?>
- </td>
- </tr>
- <tr>
- <td>Password</td>
- <td>
- <input type="password" name="password">
- </td>
- <td style="color:red;">
- <?php if (!empty($errorPassword)) { echo $errorPassword; } ?>
- </td>
- </tr><tr>
- <td>Confirm Password</td>
- <td>
- <input type="password" name="passwordConfirm">
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td colspan="2" style="text-align:right;">
- <input type="submit" name="submit" value="Register">
- </td>
- <td style="color:green;">
- <?php if (!empty($message)) { echo $message; } ?>
- </td>
- </tr>
- </table>
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement