ishaan2

hashpasserr

Aug 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. <?php
  2. //add db conn script
  3. include 'resource/utlities.php';
  4. include 'resource/database.php';
  5. //process the form
  6.  
  7. if (isset($_POST['signupBtn'])){
  8.     //initialize the array to store any message
  9.     $form_errors = array();
  10.  
  11.     //form validation
  12.     $required_fields = array('email','username','password');
  13.     // empty field
  14.     $form_errors = array_merge($form_errors, check_empty_fields($required_fields));
  15.     //min length
  16.     $field_to_check_length = array('username' => 4, 'password' => 6);
  17.     //merge min len with form err
  18.     $form_errors = array_merge($form_errors, check_min_length($field_to_check_length));
  19.     //email val merging
  20.     $form_errors = array_merge($form_errors, check_email($_POST));
  21.  
  22.  
  23.     //check if error array is empty
  24.     if (empty($form_errors)){
  25.         //collect form data and store in variables
  26.         $email = $_POST['email'];
  27.         $username = $_POST['username'];
  28.         $password = $_POST['password'];
  29. //encrypt passwor
  30.         $hash_password = password_hash($password, PASSWORD_DEFAULT);
  31.         try{
  32.             //sql insert stmt..
  33.             $sqlInsert = "INSERT INTO my_db.user2(username, password, email,join_date) VALUES(:username,:password,:email,now())";
  34.  
  35. //use pdo prepare sanitize data
  36.             $statement = $db->prepare($sqlInsert);
  37.  
  38.             $statement->execute(array(':username'=>$username, ':email'=>$email, ':password'=>$password));
  39.  
  40.             if($statement->rowCount() == 1){
  41.                 $result = "<p style = 'padding: 20px; color: green;'>Registration Successful</p>";
  42.             }
  43.  
  44.         }catch(PDOException $ex){
  45.             $result = "<p style = 'padding: 20px; color: red;'>Registration Not Successful ".$ex->getMessage()."</p>";
  46.         }
  47.     }else{
  48.         if (count($form_errors) == 1){
  49.             $result = "<p style = 'color: red;'>There is 1 error in the form<br>";
  50.  
  51.         }else{
  52.             $result = "<p style = 'color: red;'>There were ".count($form_errors)." an error in the form<br>";
  53.  
  54.         }
  55.     }
  56.  
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. ?>
  67.  
  68.  
  69.  
  70.  
  71. <!DOCTYPE html>
  72. <html>
  73. <head lang = "en">
  74. <meta charset = "UTF-8">
  75. <title>Register page</title>
  76. </head>
  77. <body>
  78.  
  79.  
  80. <h2>Homepage</h2><br>
  81.  
  82.  
  83. </body>
  84.  
  85.  
  86.  
  87.  
  88. <h3>Register Form</h3>
  89.  
  90.  
  91.  
  92. <?php if(isset($result)){
  93.                     echo $result; }
  94.  
  95. ?>
  96. <?php if (!empty($form_errors)){
  97.     echo show_errors($form_errors);
  98. }
  99. ?>
  100.  
  101. <form method = "post" action = "">
  102.     <table>
  103.         <tr>
  104.             <td>Email: </td>
  105.             <td><input type = "text" value = "" name = "email" placeholder="email"></td>
  106.         </tr>
  107.         <tr>
  108.             <td>Username: </td>
  109.             <td><input type = "text" value = "" name = "username" placeholder="username"></td>
  110.         </tr>
  111.         <tr>
  112.             <td>Password: </td>
  113.             <td><input type = "password" value = "" name = "password" placeholder="password"></td>
  114.         </tr>
  115.         <tr><td></td><td><input style = "float:right;" type = "submit" name = "signupBtn" value = "Signup"></td></tr>
  116.     </table>
  117. </form>
  118.  
  119. <p><a href = "index.php">Back</a></p>
  120.  
  121.  
  122. </html>
Add Comment
Please, Sign In to add comment