Guest User

g

a guest
Dec 25th, 2018
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.49 KB | None | 0 0
  1. <?php
  2. session_start();
  3.   if(isset($_POST)){
  4.     //make connection
  5.     $servername = "localhost";
  6.     $username = "root";
  7.     $password = "";
  8.     $dbname = "exam";
  9.  
  10.     // Create connection
  11.     $conn = new mysqli($servername, $username, $password, $dbname);
  12.     // Check connection
  13.     if ($conn->connect_error) {
  14.         die("Connection failed: " . $conn->connect_error);
  15.     }
  16.     if((isset($_POST['username']) && !empty($_POST['username'])) && (isset($_POST['password']) && !empty($_POST['password']))){
  17.     $username = trim(htmlspecialchars($_POST['username']));
  18.     $password = md5(trim($_POST['password']));
  19.     $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
  20.     $row = $conn->query($query);
  21.     if($fetch = $row->num_rows == 1){
  22.       $guka = $row->fetch_assoc();
  23.  
  24.       $_SESSION['username'] =  $guka['username'];
  25.       $_SESSION['password'] =  $guka['password'];
  26.  
  27.       print_r($_SESSION);
  28.     }else{
  29.       echo "incorrect password";
  30.     }
  31.  
  32.   }
  33. $conn->close();
  34.   }
  35. ?>
  36. <!DOCTYPE html>
  37. <html lang="en" dir="ltr">
  38.   <head>
  39.     <meta charset="utf-8">
  40.     <title></title>
  41.   </head>
  42.   <body>
  43.     <form class="" method="post">
  44.       <input type="text" name="username" placeholder="username">
  45.       <input type="password" name="password" placeholder="password">
  46.       <input type="submit" name="" value="submit">
  47.     </form>
  48.     <?php
  49.     if(isset($_SESSION)){
  50.  
  51.       echo $_SESSION['username'];
  52.     }
  53.  
  54.      ?>
  55.   </body>
  56. </html>
Add Comment
Please, Sign In to add comment