How to use session in php to prevent users from illegally logging into the backend, phpsession
The example in this article describes how to use session in PHP to prevent users from illegally logging into the backend. Share it with everyone for your reference. The details are as follows:
Generally speaking, when we log in to the backend of the website, the server will save the login information into the session file and determine whether the backend operation can be performed by reading the session file.
Take the following as an example, if admin.php is our background operation page, if session is not enabled, then the user can still access the page even if he is not logged in. At this time, you need to use session to prevent the user from accessing the page. You have logged into this page illegally. Below is the code for the three files
Login page: login.php
Copy code The code is as follows:
User login page
if(!empty($_GET['errno'])){
if($_GET['errno']==1){
echo "Wrong username or password";
}else if($_GET['errno']==2){
echo "Please enter username and password";
}else if($_GET['errno']==3){
echo "Illegal access, please enter username and password";
}
}
?>
Login information processing page: loginProcess.php
Copy code The code is as follows:
//Here we mainly talk about sessions. Regarding login information verification, the database is not involved
//Receive login information and save session
if(!empty($_POST['sub'])){
if($_POST['username']=="admin" && $_POST['pwd']=="admin"){
echo "Login successful";
Session_start();//Open session
$_SESSION['username'] = $_POST['username'];//Save the login name into the session
header("Location: admin.php");
exit();
}else{
header("Location: login.php?errno=1");
exit();
}
}else{
header("Location: login.php?errno=2");
exit();
}
?>
Backend file: admin.php
Copy code The code is as follows:
session_start();
if(empty($_SESSION['username'])){
header("Location: login.php?errno=3");
exit();
}
echo "You are the administrator, you now have background management rights";
?>
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/948403.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/948403.htmlTechArticleHow to use session in php to prevent users from illegally logging into the backend, phpsession This article describes the use of session in php to prevent users from illegally logging in. How to log in to the backend. Share it with everyone for your reference...