Advertisement
karlokokkak

Untitled

Mar 30th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.11 KB | None | 0 0
  1. <?php
  2. require_once 'connect.php';
  3. if((isset($_SESSION['user']) AND trim( $_SESSION['user'] ) != "") OR ( isset($_COOKIE['remember_me']) AND trim($_COOKIE['remember_me'] ) != "" ) ) {
  4.     header('Location: profile.php');
  5.     exit;
  6. }
  7. $error = '';
  8.  
  9. try {
  10.     if (isset($_POST['login'])) {
  11.  
  12.         $username = $_POST['username'];
  13.         $password = $_POST['password'];
  14.  
  15.         //Retrieve the field values from our registration form.
  16.         // $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
  17.         // $password = !empty($_POST['password']) ? trim($_POST['password']) : null;
  18.  
  19.  
  20. //Construct the SQL statement and prepare it.
  21.         $sql = "SELECT
  22.                id AS id,
  23.                username AS username,
  24.                password AS password,
  25.                email AS email,
  26.                phone AS phone,
  27.                address AS address,
  28.                first_name AS first_name,
  29.                last_name AS last_name,
  30.                age AS age            
  31.            FROM
  32.                users
  33.            WHERE
  34.                 username = ?
  35.             ";
  36.         $stmt = $pdo->prepare($sql);
  37.         $stmt->execute([$username]);
  38.         $user = $stmt->fetch(PDO::FETCH_ASSOC);
  39.  
  40.         $passwordHash = $user['password'];
  41.         if (!password_verify($password, $passwordHash)) {
  42.             setcookie('remember_me', '', time() - 100000);
  43.             throw new Exception("Wrong username or password!");
  44.         }
  45.         else {
  46.             if($_POST['remember']) {
  47.                 $month = time() + 3600 * 24 * 30;
  48.                 setcookie('remember_me', $_POST['username'], $month);
  49.             }
  50.             elseif(!$_POST['remember']) {
  51.                 $past = time() - 100;
  52.                 setcookie('remember_me', '', $past);
  53.             }
  54.            
  55.             $hour = time() + 3600;
  56.             setcookie('ID_my_site', $_POST['username'], $hour);
  57.  
  58.             $_SESSION['id'] = $user['id'];
  59.             $_SESSION['user'] = $user['username'];
  60.             header('Location: profile.php'); exit();
  61.         }
  62.        
  63.         $_SESSION['id'] = '';
  64.         $_SESSION['user'] = '';
  65.         header('Location: login.php');
  66.         exit;
  67.  
  68.     }
  69. } catch (Exception $exception) {
  70.     $error = $exception->getMessage();
  71. }
  72.  
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement