Advertisement
Guest User

index

a guest
Nov 17th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.82 KB | None | 0 0
  1. <?php  
  2.  session_start();  
  3.  $host = "localhost";  
  4.  $username = "root";  
  5.  $password = "";  
  6.  $database = "site";  
  7.  $message = "";  
  8.  try  
  9.  {  
  10.       $connect = new PDO("mysql:host=$host; dbname=$database", $username, $password);  
  11.       $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
  12.       if(isset($_POST["login"]))  
  13.       {  
  14.  
  15.            
  16.            if(empty($_POST["username"]) || empty($_POST["password"]))  
  17.            {  
  18.                 $message = '<label>Les champs sont requis</label>';  
  19.            }  
  20.            else  
  21.            {  
  22.                 $query = "SELECT * FROM member WHERE username = :username AND password = :password";  
  23.                 $statement = $connect->prepare($query);  
  24.                 $statement->execute(  
  25.                      array(  
  26.                           'username'     =>     $_POST["username"],  
  27.                           'password'     =>     $_POST["password"]  
  28.                      )  
  29.                 );  
  30.                 $count = $statement->rowCount();  
  31.                 if($count > 0)  
  32.                 {  
  33.                      $_SESSION["username"] = $_POST["username"];  
  34.                      header("location:login_success.php");  
  35.                 }  
  36.                 else  
  37.                 {  
  38.                      $message = '<label>Incorrect</label>';  
  39.                 }  
  40.            }  
  41.       }  
  42.  }  
  43.  catch(PDOException $error)  
  44.  {  
  45.       $message = $error->getMessage();  
  46.  }  
  47.  ?>  
  48.  <!DOCTYPE html>  
  49.  <html>  
  50.       <head>  
  51.            <title>Login</title>  
  52.            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
  53.            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
  54.            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
  55.       </head>  
  56.       <body>  
  57.            <br />  
  58.            <div class="container" style="width:500px;">  
  59.                 <?php  
  60.                 if(isset($message))  
  61.                 {  
  62.                      echo '<label class="text-danger">'.$message.'</label>';  
  63.                 }  
  64.                 ?>  
  65.                 <h3 align="">PHP Login Script using PDO</h3><br />  
  66.                 <form method="post">  
  67.                      <label>Username</label>  
  68.                      <input type="text" name="username" class="form-control" />  
  69.                      <br />  
  70.                      <label>Password</label>  
  71.                      <input type="password" name="password" class="form-control" />  
  72.                      <br />  
  73.                      <input type="submit" name="login" class="btn btn-info" value="Login" />  
  74.                 </form>  
  75.            </div>  
  76.            <br />  
  77.       </body>  
  78.  </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement