Guest User

Untitled

a guest
May 16th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.46 KB | None | 0 0
  1. /**
  2.  * If user trying to log in
  3.  * into admin panel
  4.  */
  5. if ($_SERVER['REQUEST_METHOD'] == 'POST'
  6.         && $_POST['submit'] == 'Prisijungti'
  7.         && !empty ($_POST['username'])
  8.         && !empty ($_POST['password'])) {
  9.    
  10.     // include neccesary files
  11.     include_once '../inc/db.inc.php';
  12.    
  13.     // database connection
  14.     $db = new PDO(DB_INFO, DB_USER, DB_PASS);
  15.    
  16.     $sql = "SELECT COUNT(*) as num_users
  17.            FROM users
  18.            WHERE username = ? AND password = ?";
  19.    
  20.     $stmt = $db->prepare($sql);
  21.     $stmt->execute(array(
  22.         mysql_real_escape_string($_POST['username']),
  23.         mysql_real_escape_string(md5($_POST['password']))
  24.     ));
  25.    
  26.     $response = $stmt->fetch();
  27.    
  28.     $stmt->closeCursor();
  29.    
  30.     if ($response['num_users'] > 0) {        
  31.         /**
  32.          * if logged in succesfully redirect user to the admin panel
  33.          * also create a session
  34.          */
  35.         $_SESSION['loggedIn'] = TRUE;
  36.         $_SESSION['username'] = mysql_escape_string($_POST['username']);
  37.         $_SESSION['lastLogin'] = date();
  38.        
  39.         header('Location: ../admin/admin.php');
  40.     } else {
  41.         /**
  42.          * destroy the session
  43.          * and print out the error message
  44.          */
  45.         session_unset();
  46.         session_destroy();
  47.        
  48.         echo '<p class="error">Something wrong with your login information. Please <a href="../admin">try</a> again</a>';
  49.     }
  50.    
  51. }
Add Comment
Please, Sign In to add comment