PHP Prevent the Return to Login Page/Disable Back after Login

Hello. I know people are wondering how to disable back button or preventing return to login page after login. Actually there's no method to disable back or preventing return but this tutorial will teach you a simple trick by use of session. I've created a simple login in this tutorial but if you want, you can learn How to Create a Simple Login with Validation. So. let's start the tutorial.

Creating our Database

First, we're going to create our database that contains our sample users. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as "sample". 3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
  1. CREATE TABLE `user` (
  2.   `userid` INT(11) NOT NULL AUTO_INCREMENT,
  3.   `username` VARCHAR(30) NOT NULL,
  4.   `password` VARCHAR(12) NOT NULL,
  5. PRIMARY KEY(`userid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
back

Inserting Data into our Database

Next, we insert sample data into our database to use in our login. 1. Click our database "sample". 2. Click SQL and paste the below code.
  1. INSERT INTO `user` (`username`, `password`) VALUES
  2. ('admin', 'admin'),
  3. ('user', 'user');

Creating our Connection

Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.
  1. <?php
  2.  
  3. $conn = mysqli_connect("localhost","root","","sample");
  4. if (!$conn) {
  5.         die("Connection failed: " . mysqli_connect_error());
  6. }
  7.  
  8. ?>

Creating our Login Page

Next step is to create our login page. As I've said, this is just a simple login. We name this as "index.php".
  1. <?php
  2.         session_start();
  3.         include('conn.php');
  4.        
  5.         if (isset($_SESSION['id'])){
  6.                 header('location:goto.php');
  7.         }
  8. ?>
  9. <!DOCTYPE html>
  10. <html>
  11. <head>
  12.         <title>PHP Prevent the Returning to Login Page after Login</title>
  13. </head>
  14.         <h2>Sample Login</h2>
  15.         <form method="POST" action="login.php">
  16.                 <input type="text" name="username" placeholder="Username"><br><br>
  17.                 <input type="password" name="password" placeholder="Password"><br><br>
  18.                 <input type="submit" value="Login"><br><br>
  19.         </form>
  20.         <?php
  21.        
  22.                 if (isset($_SESSION['msg'])){
  23.                         echo $_SESSION['msg'];
  24.                         unset($_SESSION['msg']);
  25.                 }
  26.        
  27.         ?>
  28. </html>

Creating our Login Code

Next, we create our login code to validate our login. We name this as "login.php".
  1. <?php
  2.         session_start();
  3.         include('conn.php');
  4.         if($_SERVER["REQUEST_METHOD"] == "POST"){
  5.                        
  6.                 $username=$_POST['username'];
  7.                 $password=$_POST['password'];
  8.                        
  9.                 $query=mysqli_query($conn,"select * from `user` where username='$username' and password='$password'");
  10.                 if (mysqli_num_rows($query)<1){
  11.                         $_SESSION['msg']="Login Failed, User not found!";
  12.                         header('location:index.php');
  13.                 }
  14.                 else{
  15.                         $row=mysqli_fetch_array($query);
  16.                         $_SESSION['id']=$row['userid'];
  17.                         header('location: goto.php');
  18.                         }
  19.                 }
  20.                
  21. ?>

Creating our Goto Page

Next step is to create our goto page after a successful login. We name this as our "goto.php".
  1. <?php
  2.         session_start();
  3.        
  4.         if (!isset($_SESSION['id']) ||(trim ($_SESSION['id']) == '')) {
  5.                 header('location:index.php');
  6.                 exit();
  7.         }
  8. ?>
  9. <!DOCTYPE html>
  10. <html>
  11. <head>
  12.         <title>PHP Prevent the Returning to Login Page after Login</title>
  13. </head>
  14. <body>
  15.         <?php
  16.                 include('conn.php');
  17.                 $query=mysqli_query($conn,"select * from `user` where userid='".$_SESSION['id']."'");
  18.                 $row=mysqli_fetch_array($query);
  19.        
  20.                 echo 'Welcome - '.$row['username'];
  21.         ?>
  22.         <br>
  23.         <a href="index.php">Back to Login</a> <a href="logout.php">Logout</a>
  24. </body>
  25. </html>

Creating our Logout

Lastly, we create our logout which destroys our session and return as back to our login page. We name this as "logout.php".
  1. <?php
  2.         session_start();
  3.         session_destroy();
  4.         header('location:index.php');
  5. ?>
That ends this tutorial. You can test the code by clicking the back button that I've created in the goto page or the back button of the browser. If you have any question or suggestion, feel free to comment below or send me a message. Happy Coding :)

Comments

One of the best ways I found out after so many trials. Thanks a lot for sharing.

Add new comment