Advertisement
Guest User

Untitled

a guest
Apr 9th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.30 KB | None | 0 0
  1. <?php
  2. $error = false;
  3. $message = $errorUsername = $errorEmail = $errorPassword = "";
  4.  
  5. if (isset($_POST['submit'])) {
  6.  
  7.     // Check username criteria
  8.     if (empty($_POST['username'])) {
  9.         $errorUsername .= "No username written! ";
  10.         $error = true;
  11.  
  12.     } else {
  13.         if (!preg_match('/^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/', $_POST['username'])) {
  14.             $errorUsername .= "Not a valid username! ";
  15.             $error = true;
  16.         } else {
  17.             $username = htmlentities($_POST['username']);
  18.  
  19.             // Checks if username is atleast 3 characters
  20.             if (strlen($username) < 3) {
  21.                 $errorUsername .= "The username is too short. Minimum 3 characters! ";
  22.                 $error = true;
  23.  
  24.                 // Checks if username is too long
  25.             } else if (strlen($username) > 32) {
  26.                 $errorUsername .= "The username is too long. Maximum 32 characters! ";
  27.                 $error = true;
  28.  
  29.             } else {
  30.                 try {
  31.                     $stmt = $db->prepare("SELECT username FROM users WHERE username=:username");
  32.                     $stmt->bindParam(":username", $username, PDO::PARAM_STR);
  33.                     $stmt->execute();
  34.  
  35.                     // Checks if username exists in the database
  36.                     if ($stmt->rowCount() > 0) {
  37.                         $errorUsername .= "That username is already taken! ";
  38.                         $error = true;
  39.                     }
  40.                 } catch (PDOException $e) {
  41.                     echo $e->getMessage() . PHP_EOL;
  42.                     $error = true;
  43.                 }
  44.             }
  45.         }
  46.     }
  47.  
  48.     // Checks if email is typed
  49.     if (empty($_POST['email'])) {
  50.         $errorEmail .= "No email written! ";
  51.         $error = true;
  52.  
  53.     } else {
  54.         $email = htmlentities($_POST['email']);
  55.  
  56.         // Checks if email is a valid email
  57.         if (!(filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/@.+\./', $email))) {
  58.             $errorEmail .= "Not a valid email address! ";
  59.             $error = true;
  60.  
  61.         } else {
  62.             try {
  63.                 $stmt = $db->prepare("SELECT email FROM users WHERE email=:email");
  64.                 $stmt->bindParam(":email", $email, PDO::PARAM_STR);
  65.                 $stmt->execute();
  66.  
  67.                 // Checks if email exists in database
  68.                 if ($stmt->rowCount() > 0) {
  69.                     $errorEmail .= "That email address is already in use! ";
  70.                     $error = true;
  71.                 }
  72.             } catch (PDOException $e) {
  73.                 echo $e->getMessage();
  74.                 $error = true;
  75.             }
  76.         }
  77.     }
  78.  
  79.     // Checks if password is typed
  80.     if (empty($_POST['password'])) {
  81.         $errorPassword .= "No password written! ";
  82.         $error = true;
  83.  
  84.     } else {
  85.         // Checks if second password is typed
  86.         if (empty($_POST['passwordConfirm'])) {
  87.             $errorPassword .= "No confirmed password written! ";
  88.             $error = true;
  89.         }
  90.  
  91.         // Checks if passwords match
  92.         if ($_POST['password'] != $_POST['passwordConfirm']) {
  93.             $errorPassword .= "Passwords does not match! ";
  94.             $error = true;
  95.  
  96.         // Checks if password is atleast 8 characters
  97.         } else if (strlen($_POST['password']) <= 8) {
  98.             $errorPassword .= "Password is too short. Minimum 8 characters! ";
  99.             $error = true;
  100.         }
  101.  
  102.     }
  103.  
  104.  
  105.     // Now we can create user and do the query!
  106.     if (!$error) {
  107.         // Hashes the password
  108.         $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
  109.         // Create date (1992-05-14)
  110.         $date = date("Y-m-d");
  111.  
  112.         echo $username . "<br />";
  113.         echo $email . "<br />";
  114.         echo $password . "<br />";
  115.         echo $date . "<br />";
  116.  
  117.         try {
  118.             $stmt = $db->prepare("INSERT INTO users (username, email, password, created) VALUES (:username, :email, :password, :created)");
  119.             $stmt->bindParam(":username", $username, PDO::PARAM_STR);
  120.             $stmt->bindParam(":email", $email, PDO::PARAM_STR);
  121.             $stmt->bindParam(":password", $password, PDO::PARAM_STR);
  122.             $stmt->bindParam(":created", $date, PDO::PARAM_STR);
  123.             $stmt->execute();
  124.  
  125.             $message .= "User is made!";
  126.         } catch (PDOException $e) {
  127.             echo $e->getMessage() . PHP_EOL;
  128.         }
  129.     }
  130.  
  131. }
  132.  
  133.  
  134.  
  135. ?>
  136.  
  137. <ul>
  138.     <?php if (!$_SESSION['user']) { ?>
  139.         <li><a href="index.php">Home</a></li>
  140.         <li><a href="login.php">Login</a></li>
  141.     <?php } else { ?>
  142.         <li><a href="index.php">Home</a></li>
  143.         <li><a href="logout.php">Logout</a></li>
  144.     <?php } ?>
  145. </ul>
  146.  
  147. <form action="register.php" method="post" name="register">
  148.  
  149.     <table>
  150.         <tr>
  151.             <td>Username</td>
  152.             <td>
  153.                 <input type="text" name="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>">
  154.             </td>
  155.             <td style="color:red;">
  156.                 <?php if (!empty($errorUsername)) { echo $errorUsername; } ?>
  157.             </td>
  158.         </tr>
  159.         <tr>
  160.             <td>Email</td>
  161.             <td>
  162.                 <input type="email" name="email" value="<?php if (isset($_POST['email'])) echo htmlentities($_POST['email']); ?>">
  163.             </td>
  164.             <td style="color:red;">
  165.                 <?php if (!empty($errorEmail)) { echo $errorEmail; } ?>
  166.             </td>
  167.         </tr>
  168.         <tr>
  169.             <td>Password</td>
  170.             <td>
  171.                 <input type="password" name="password">
  172.             </td>
  173.             <td style="color:red;">
  174.                 <?php if (!empty($errorPassword)) { echo $errorPassword; } ?>
  175.             </td>
  176.         </tr><tr>
  177.             <td>Confirm Password</td>
  178.             <td>
  179.                 <input type="password" name="passwordConfirm">
  180.             </td>
  181.             <td>
  182.  
  183.             </td>
  184.         </tr>
  185.         <tr>
  186.             <td colspan="2" style="text-align:right;">
  187.                 <input type="submit" name="submit" value="Register">
  188.             </td>
  189.             <td style="color:green;">
  190.                 <?php if (!empty($message)) { echo $message; } ?>
  191.             </td>
  192.         </tr>
  193.     </table>
  194.  
  195. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement