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

Submitted by: 
Visitors have accessed this post 1523 times.

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`)


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
  3. $conn = mysqli_connect("localhost","root","","sample");
  4. if (!$conn) {
  5. die("Connection failed: " . mysqli_connect_error());
  6. }
  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. include('conn.php');
  4. if (isset($_SESSION['id'])){
  5. header('location:goto.php');
  6. }
  7. ?>
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <title>PHP Prevent the Returning to Login Page after Login</title>
  12. </head>
  13. <h2>Sample Login</h2>
  14. <form method="POST" action="login.php">
  15. <input type="text" name="username" placeholder="Username"><br><br>
  16. <input type="password" name="password" placeholder="Password"><br><br>
  17. <input type="submit" value="Login"><br><br>
  18. </form>
  19. <?php
  21. if (isset($_SESSION['msg'])){
  22. echo $_SESSION['msg'];
  23. unset($_SESSION['msg']);
  24. }
  26. ?>
  27. </html>

Creating our Login Code

Next, we create our login code to validate our login. We name this as "login.php".

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

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

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 :)

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.