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.
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 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.
INSERT INTO `user` (`username`, `password`) VALUES
('admin', 'admin'),
('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.
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".
<?php
include('conn.php');
if (isset($_SESSION['id'])){
}
?>
<!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'];
}
?>
</html>
Creating our Login Code
Next, we create our login code to validate our login. We name this as "login.php".
<?php
include('conn.php');
if($_SERVER["REQUEST_METHOD"] == "POST"){
$username=$_POST['username'];
$password=$_POST['password'];
$query=mysqli_query($conn,"select * from `user` where username='$username' and password='$password'");
$_SESSION['msg']="Login Failed, User not found!";
}
else{
$_SESSION['id']=$row['userid'];
}
}
?>
Creating our Goto Page
Next step is to create our goto page after a successful login. We name this as our "goto.php".
<?php
if (!isset($_SESSION['id']) ||(trim ($_SESSION['id']) == '')) {
}
?>
<!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']."'");
echo 'Welcome - '.$row['username'];
?>
<br>
<a href="index.php">Back to Login</a> <a href="logout.php">Logout</a>
</body>
</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".
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 :)