Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.28 KB | None | 0 0
  1. <?php
  2.  
  3. $host = "localhost";
  4. $dbname = "tennis";
  5. $username = "root";
  6. $password = "";
  7.  
  8. //Start the Session
  9. session_start();
  10.  
  11. $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
  12.  
  13.  
  14. //3. If the form is submitted or not.
  15. //3.1 If the form is submitted
  16. if (isset($_POST['email']) and isset($_POST['password'])) {
  17.  
  18.     // later add more validation - filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)
  19.  
  20.     $errors = [];
  21.         if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  22.             $errors['email'] = 'Email ni pravilen.';
  23.         }
  24.    
  25.         if (empty($_POST['email']) || empty($_POST['password'])) {
  26.             $errors['empty'] = 'Email in geslo sta obvezna.';
  27.         }
  28.  
  29.     //3.1.1 Assigning posted values to variables.
  30.     $email = $_POST['email'];
  31.     $password = $_POST['password'];
  32.  
  33.     //3.1.2 Checking the values are existing in the database or not
  34.    
  35.  
  36.     if (count($errors) > 0) {
  37.         $errors = ['email' => $email, 'password' => $password];
  38.         require 'login.phtml';
  39.         die;
  40.     }
  41.    
  42.     $result = $pdo->query("SELECT * FROM users WHERE email = :email and password = :password")->fetch(PDO::FETCH_ASSOC);
  43.    
  44.     if ($result == 1) {
  45.         $_SESSION['username'] = $username;
  46.     } else {
  47.         $fmsg = "Invalid Login Credentials.";
  48.     }
  49. }
  50.        
  51.         if (empty($result)) {
  52.             $errors['result'] = 'Uporabnik s tem emailom in geslom v bazi ne obstaja.';
  53.             die;
  54.         }
  55.    
  56.        
  57.     // poglej, da result ni prazen (da si iz baze dobila uporabnika)
  58.  
  59.  
  60.     // poglej kako sva errorje prikazovala in dodajala
  61.  
  62.  
  63.     // shrani ime uporabnika v session
  64.     //3.1.2 If the posted values are equal to the database values, then session will be created for the user.
  65.    
  66.         //3.1.3 If the login credentials doesn't match, he will be shown with an error message.
  67.        
  68.  
  69. //3.1.4 if the user is logged in Greets the user with message
  70. if (isset($_SESSION['username'])) {
  71.     $username = $_SESSION['username'];
  72.     echo "Živjo, " . $username;
  73.     echo "This is the Members Area";
  74.     echo "<a href='logout.php'>Logout</a>";
  75. } else {
  76. //3.2 When the user visits the page first time, simple login form will be displayed.
  77.     require 'login.phtml';
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement