ishaan2

index error ish

Aug 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. <?php
  2. include 'resource/session.php';
  3. include 'resource/database.php';
  4. include 'resource/utlities.php';
  5.  
  6. if (isset($_POST['loginBtn'])){
  7.     $form_errors = array();
  8. //validate
  9.     $required_fields = array('username', 'password');
  10.     $form_errors = array_merge($form_errors, check_empty_fields($required_fields));
  11.     if (empty($form_errors)){
  12.         //check user exist
  13.         //collect data
  14.         $user = $_POST['username'];
  15.         $password = $_POST['password'];
  16.         $sqlQuery = "SELECT * FROM my_db user2 WHERE username = :username";
  17.         $statement = $db->prepare($sqlQuery);
  18.         $statement->execute(array(':username'=> $user));
  19.  
  20.         while($row = $statement->fetch()){
  21.             $id = $row['id'];
  22.             $hashed_password = $row['password'];
  23.             $username = $row['username'];
  24.  
  25.             if (password_verify($password, $hashed_password)){
  26.                 $_SESSION['id'] = $id;
  27.                 $_SESSION['username'] = $username;
  28.                 header('Location: index.php');
  29.             }else{
  30.                 $result = "<p style='padding: 20px; color: red; border: 1px solid gray;'>Invalid username or password</p>";
  31.             }
  32.         }
  33.  
  34.     }else{
  35.         if(count($form_errors) == 1){
  36.             $result = "<p style='color:red;'>There was 1 error in the form</p>";
  37.         }else{
  38.             $result = "<p style='color:red;'>There were ".count($form_errors)." errors in the form</p>";
  39.         }
  40.     }
  41.  
  42.  
  43. }
  44.  
  45.  
  46.  
  47. ?>
  48.  
  49.  
  50.  
  51. <!DOCTYPE html>
  52. <html>
  53. <head lang = "en">
  54. <meta charset = "UTF-8">
  55. <title>Loginpage</title>
  56. </head>
  57. <body>
  58.  
  59.  
  60. <h2>User Authentication System</h2><br>
  61.  
  62.  
  63. </body>
  64.  
  65.  
  66.  
  67. <h3>login Form</h3>
  68.  
  69. <?php
  70. if (isset($result)) echo $result;
  71.  
  72. ?>
  73. <?php
  74.  
  75. if (!empty($form_errors))
  76.     echo show_errors($form_errors);
  77.  
  78.  
  79. ?>
  80.  
  81. <form method = "post" action = "">
  82.     <table>
  83.         <tr>
  84.             <td>Username: </td>
  85.             <td><input type = "text" value = "" name="username"></td>
  86.         </tr>
  87.         <tr>
  88.             <td>Password: </td>
  89.             <td><input type = "password" value = "" name="password"></td>
  90.         </tr>
  91.         <tr><td></td><td><input style = "float:right;" type = "submit" name="loginBtn" value = "Signin"></td></tr>
  92.     </table>
  93. </form>
  94.  
  95. <p><a href = "index.php">Back</a></p>
  96.  
  97.  
  98. </html>
Add Comment
Please, Sign In to add comment