PHP - Simple Autologout When Inactive

In this tutorial we will create a Simple Autologout When Inactive. PHP is a server-side scripting language designed primarily for web development. It is a lean and consistent way to access databases. This means developers can write portable code much easier. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_logout, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'db_logout');
  3.  
  4. if(!$conn){
  5. die("Error: Can't connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as shown below. index.php
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Autologout When Inactive</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add User</button>
  18. <br style="clear:both;" /><br /><br />
  19. <div class="col-md-2"></div>
  20. <div class="col-md-8">
  21. <div class="alert alert-info">Login</div>
  22.  
  23. <form method="POST" action="login.php">
  24. <div class="form-group">
  25. <label>Username</label>
  26. <input type="text" name="username" class="form-control"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Password</label>
  30. <input type="password" name="password" class="form-control"/>
  31. </div>
  32. <br />
  33. <button class="btn btn-primary form-control" name="login"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  34. </form>
  35. </div>
  36. </div>
  37. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  38. <div class="modal-dialog" role="document">
  39. <form action="save_query.php" method="POST">
  40. <div class="modal-content">
  41. <div class="modal-body">
  42. <div class="col-md-2"></div>
  43. <div class="col-md-8">
  44. <div class="form-group">
  45. <label>Username</label>
  46. <input class="form-control" type="text" name="username">
  47. </div>
  48. <div class="form-group">
  49. <label>Password</label>
  50. <input class="form-control" type="password" name="password" />
  51. </div>
  52. <div class="form-group">
  53. <label>Name</label>
  54. <input class="form-control" type="text" name="name" />
  55. </div>
  56. </div>
  57. </div>
  58. <div style="clear:both;"></div>
  59. <div class="modal-footer">
  60. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  61. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  62. </div>
  63. </div>
  64. </form>
  65. </div>
  66. </div>
  67. </body>
  68. <script src="js/jquery-3.2.1.min.js"></script>
  69. <script src="js/bootstrap.js"></script>
  70. </html>
home.php
  1. <!DOCTYPE html>
  2. <?php
  3. if(!ISSET($_SESSION['user_id'])){
  4. header('location:index.php');
  5. }else{
  6. if((time() - $_SESSION['time']) > 180){
  7. header('location: logout_page.php');
  8. }
  9. }
  10. ?>
  11. <html lang="en">
  12. <head>
  13. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  14. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  15. </head>
  16. <body>
  17. <nav class="navbar navbar-default">
  18. <div class="container-fluid">
  19. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  20. </div>
  21. </nav>
  22. <div class="col-md-3"></div>
  23. <div class="col-md-6 well">
  24. <h3 class="text-primary">PHP - Simple Autologout When Inactive</h3>
  25. <hr style="border-top:1px dotted #ccc;"/>
  26. <?php
  27. require 'conn.php';
  28. $query = $conn->query("SELECT * FROM `user` WHERE `user_id` = '$_SESSION[user_id]'");
  29. $fetch = $query->fetch_array();
  30. ?>
  31. <h2 class="text-success">Welcome!</h2>
  32. <br />
  33. <center><h1><?php echo $fetch['name']?></h1></center>
  34. <a href="logout.php">Logout</a>
  35. </div>
  36. </body>
  37. </html>
logout_page.php
  1. <!DOCTYPE html>
  2. <?php
  3. ?>
  4. <html lang="en">
  5. <head>
  6. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  7. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  8. </head>
  9. <body>
  10. <nav class="navbar navbar-default">
  11. <div class="container-fluid">
  12. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  13. </div>
  14. </nav>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">PHP - Simple Autologout When Inactive</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <center><h3>You have been logout for 3 minutes inactive!</h3></center>
  20. <a href="index.php">Back to login</a>
  21. </div>
  22. </body>
  23. </html>

Creating PHP Functions

This code contains the main function of the application. This code is consist of several function. It will force the user to logout when stay in an inactive state. To do that just copy and write these block of codes inside the text editor, then save it as shown below. save_query.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $username = $_POST['username'];
  6. $password = $_POST['password'];
  7. $name = $_POST['name'];
  8.  
  9. $conn = $conn->query("INSERT INTO `user` VALUES('', '$username', '$password', '$name')");
  10.  
  11. header('location: index.php');
  12. }
  13. ?>
login.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['login'])){
  5. if(!empty($_POST['username']) && !empty($_POST['password'])){
  6. $username = $_POST['username'];
  7. $password = $_POST['password'];
  8.  
  9. $query = $conn->query("SELECT * FROM `user` WHERE `username` = '$username' && `password` = '$password' ") or die(mysqli_errno());
  10. $row = $query->num_rows;
  11. $fetch = $query->fetch_array();
  12. if($row > 0){
  13. $_SESSION['user_id'] = $fetch['user_id'];
  14. $_SESSION['time'] = time();
  15. echo "<script>alert('Login Confirmed!')</script>";
  16. echo "<script>window.location='home.php'</script>";
  17. }else{
  18. echo "<script>alert('Invalid username or password')</script>";
  19. echo "<script>window.location='index.php'</script>";
  20. }
  21. }else{
  22. echo "<script>alert('Please complete the requiered field!')</script>";
  23. echo "<script>window.location='index.php'</script>";
  24. }
  25.  
  26. };
  27. ?>
logout.php
  1. <?php
  2. header('location: index.php');
  3. ?>
There you have it we successfully created Simple Autologout When Inactive. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment