Advertisement
Guest User

login

a guest
Oct 26th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.83 KB | None | 0 0
  1. <?php
  2.     $server = 'localhost';
  3.     $username = 'username';
  4.     $password = 'password';
  5.     $db = 'database';
  6.    
  7.     $conn = mysqli_connect($server,$username,$password,$db);
  8.     if(!$conn){
  9.     die("Connection Failed!:".mysqli_connect_error());
  10. }
  11. ?>
  12.  
  13. <?php session_start();
  14.      if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) { //TO CHECK IF LAST REQUEST WAS MORE THAN 30 SECONDS AGO
  15.     session_unset();     // unset $_SESSION variable for the run-time
  16.     session_destroy();   // destroy session data in storage, log out user
  17.     }
  18.     $_SESSION['LAST_ACTIVITY'] = time(); // UPDATE LAST ACTIVITY STAMP
  19. ?>
  20. <!--you can puth the above two php code in seperate files and include them at the top of every page-->
  21.  
  22. <?php  
  23.     if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {  //if a user is logged in
  24.         echo "<script type='text/javascript'> document.location = 'panel.php'; </script>"; //using javscript to redirect
  25.       //  header('Location: panel.php');  //using php to redirect
  26.         //you may choose any of the two redirect options above
  27.     } else { //IF NO USER LOGGED IN
  28.         //do nothing
  29.     }
  30.     //this php code above detects if a user is logged in and session is true
  31. ?>
  32.  
  33.  <?php
  34. if(isset($_POST{'signin_submit'})){ //IF LOGIN BTN WAS CLICKED AND SENT TO THIS PAGE
  35.     if(!empty($_POST{'user_email'}) && !empty($_POST{'user_password'})){ //CHECK IF EMAIL AND PASSWORD IS NOT EMPTY
  36.         $get_user_email = $_POST['user_email'];
  37.         $get_user_email = mysqli_real_escape_string($conn,$get_user_email);
  38.         $get_password = $_POST['user_password'];
  39.         $sql = "SELECT * FROM users WHERE email = '$get_user_email' AND password = '$get_password'";
  40.         if($result1 = mysqli_query($conn,$sql)){ //FOR USERS IF THERE IS CONNECTION TO THE DATABASE WHERE EMAIL AND PASSWORD IS AVAILABLE
  41.             if(mysqli_num_rows($result1) == 1){ //IF NO. OF ROWS WITH ABOVE QUERY IS JUST ONE
  42.                  $_SESSION['loggedin'] = true;
  43.                 $_SESSION['user_email'] = $get_user_email; // $username coming from the form, such as $_POST['username']
  44.                 $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
  45.                // $inno_sql = mysqli_query($conn,$sql);
  46.                 while($rows = mysqli_fetch_assoc($result1)){ //RETRIEVE USER DETAILS
  47.                     //this session variables below can be used in the entire application to represent the users data it equates to
  48.                     $_SESSION['name'] = $rows['name'];
  49.                     $_SESSION['id'] = $rows['id'];
  50.                     $_SESSION['created_at'] = $rows['created_at'];
  51.                     $_SESSION['updated_at'] = $rows['updated_at'];
  52.                 }
  53.                 header('Location: dashboard.php');
  54.                    
  55.             } else{
  56.                 echo "<script type='text/javascript'> document.location = 'signin.php?login_error=wrong'; </script>";
  57.               //  header('Location: signin.php?login_error=wrong');
  58.             } //
  59.         } else{ //if sql query is incorrect
  60.             header('Location: signin.php?login_error=query_error');
  61.         }
  62.     }else{ //if email or password is empty
  63.         header('Location: signin.php?login_error=empty');
  64.     }
  65. }else{
  66.     $login_err = ''; //set this variable to empty string if there is no login request
  67. }
  68.         if(isset($_GET['login_error'])){ //TO OUTPUT LOGIN ERROR
  69.             if($_GET['login_error'] == 'empty'){  //LOGIN ERROR FOR EMPTY
  70.                 $login_err = "<div class='alert alert-danger'>Email or password was empty!</div>";
  71.             }elseif($_GET['login_error'] == 'wrong'){ //LOGIN ERROR FOR INVALID DETAILS
  72.                 $login_err = "<div class='alert alert-warning'>Invalid email or password!</div>";
  73.             }
  74.         }
  75.    echo $login_err;  //show login error in browser  
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement