Table of Contents
How Prevent the Return to Login Page/Disable Back after Login in PHP with Source Code
Hello. I know people are wondering how to How Prevent the Return to Login Page/Disable Back after Login. There’s no method to disable again or preventing recurrence, but this tutorial will teach you a simple trick by use of the 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 a 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.
CREATE TABLE `user` ( `userid` INT(11) NOT NULL AUTO_INCREMENT, `username` VARCHAR(30) NOT NULL, `password` VARCHAR(12) NOT NULL, PRIMARY KEY(`userid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Inserting Data into a 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.
INSERT INTO `user` (`username`, `password`) VALUES ('admin', 'admin'), ('user', 'user');
Creating a 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.
<?php $conn = mysqli_connect("localhost","root","","sample"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } ?>
Creating a Login Page – Prevent the Return to Login Page/Disable Back after Login
Next step is to create our login page. As I’ve said, this is just a simple login. We name this as “index.php”.
<?php session_start(); include('conn.php'); if (isset($_SESSION['id'])){ header('location:goto.php'); } ?> <!DOCTYPE html> <html> <head> <title>PHP Prevent the Returning to Login Page after Login</title> </head> <h2>Sample Login</h2> <form method="POST" action="login.php"> <input type="text" name="username" placeholder="Username"><br><br> <input type="password" name="password" placeholder="Password"><br><br> <input type="submit" value="Login"><br><br> </form> <?php if (isset($_SESSION['msg'])){ echo $_SESSION['msg']; unset($_SESSION['msg']); } ?> </html>
Creating a Goto Page
Next step is to create our goto page after a successful login. We name this as our “goto.php”.
<?php session_start(); if (!isset($_SESSION['id']) ||(trim ($_SESSION['id']) == '')) { header('location:index.php'); exit(); } ?> <!DOCTYPE html> <html> <head> <title>PHP Prevent the Returning to Login Page after Login</title> </head> <body> <?php include('conn.php'); $query=mysqli_query($conn,"select * from `user` where userid='".$_SESSION['id']."'"); $row=mysqli_fetch_array($query); echo 'Welcome - '.$row['username']; ?> <br> <a href="index.php">Back to Login</a> <a href="logout.php">Logout</a> </body> </html>
Creating a Logout
Lastly, we create our logout which destroys our session and return as back to our login page. We name this as “logout.php”.
<?php session_start(); session_destroy(); header('location:index.php'); ?>
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 :)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!
Download Here