Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. <?php
  2. //start session
  3. session_start();
  4.  
  5. //database connection
  6. $conn = mysqli_connect("localhost","root","","test");
  7.  
  8. //default timezone
  9. date_default_timezone_set('Asia/Kuala_Lumpur');
  10.  
  11. //if user click login button
  12. if(!empty($_POST["login"]))
  13. {
  14. //query table to verify inserted value
  15. $result = mysqli_query($conn,"SELECT * FROM users WHERE username = '" . $_POST["user_name"] . "' and password = '". $_POST["password"]."'");
  16.  
  17. //fetch result result row as an associative, a numeric array, or both
  18. $row = mysqli_fetch_array($result);
  19.  
  20. //if it is true
  21. if($row)
  22. {
  23. //declare a session for selected value using id and time logged in
  24. $_SESSION["user_id"] = $row['id'];
  25. $_SESSION['timestamp'] = time();
  26. }
  27. else
  28. {
  29. //redirect to homepage
  30. echo '<script type="text/javascript">alert("Invalid Username or Password!");window.location = "userlogin_session.php";</script>';
  31. }
  32. }
  33.  
  34. //check for session timeout
  35. if(isset($_SESSION['timestamp']))
  36. {
  37. //set time limit in seconds
  38. $expireAfterSeconds = 10;
  39.  
  40. //calculate many seconds have passed since the user was last active
  41. $secondsInactive = time() - $_SESSION['timestamp'];
  42.  
  43. //convert seconds into minutes
  44. $expireAfter = $expireAfterSeconds / 60 ;
  45.  
  46. //check to see if time is equals or above given time limit
  47. if($secondsInactive >= $expireAfter)
  48. {
  49. //kill session.
  50. session_unset();
  51. session_destroy();
  52.  
  53. //redirect to homepage
  54. echo '<script type="text/javascript">alert("Session Over");window.location = "userlogin_session.php";</script>';
  55. }
  56. }
  57.  
  58. //if user click logout button
  59. if(!empty($_POST["logout"]))
  60. {
  61. //kill session.
  62. session_unset();
  63. session_destroy();
  64. }
  65. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement