Advertisement
Guest User

login.php

a guest
Mar 19th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2.     session_start();
  3. ?>
  4.  
  5. <!DOCTYPE html>
  6. <html>
  7.     <head>
  8.         <title>Login</title>
  9.     </head>
  10.     <body>
  11.         <form method="post">
  12.             <label>Username:</label>
  13.             <input type="text" name="username" id="username" placeholder="username">
  14.             <br>
  15.             <label>Password:</label>
  16.             <input type="password" name="password" id="password" placeholder="******">
  17.             <br>
  18.             <input type="submit" name="submit" id="submit" value="Login">
  19.         </form>
  20.         <?php
  21.             if (isset($_POST['submit'])) {
  22.                 //database
  23.                 include("../secure/database.php");
  24.                 //connecting to server
  25.                 $link=mysqli_connect(HOST,USERNAME,PASSWORD,DBNAME) or die("Connect Error ".mysqli_error($link));
  26.                 if (empty($_POST['username']) || empty($_POST['password'])) {
  27.                     echo  "Username or Password is empty";
  28.                 }
  29.                 else{
  30.                         findUser(htmlspecialchars($_POST['password']),htmlspecialchars($_POST['username']),$link);
  31.                     }
  32.                 }
  33.                     function findUser($password, $username,$link){
  34.  
  35.                         $query="SELECT password FROM login WHERE user_name=?";
  36.                         $stmt = $link->stmt_init();
  37.                         $stmt= $link->prepare($query) or die("Prepare error: ".mysqli_error($link));
  38.  
  39.                             $stmt->bind_param("s", $username)  or die("Bind error".mysqli_error($link));
  40.  
  41.                             $stmt->execute() or die("Execution error".mysqli_error($link));
  42.  
  43.                             $result = mysqli_stmt_get_result($stmt);
  44.  
  45.                             print_r($result);
  46.  
  47.                             while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
  48.                                 $password_hash=$row['password'];
  49.                                 echo $row['password'];
  50.                             }
  51.                             //return the result
  52.                             if($result->num_rows==1){
  53.                                 if(password_verify($password,$password_hash)){
  54.                                     $_SESSION['login_user']=$username;
  55.                                     mysqli_close($link);
  56.                                     header('location: profile.php');
  57.                                 }
  58.                             }
  59.                             else{
  60.                                 echo 'Username or Password is invalid';
  61.                             }
  62.                         }
  63.         ?>
  64.     </body>
  65. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement