How to use session in php to prevent users from illegally logging into the background, phpsession_PHP tutorial

WBOY
Release: 2016-07-13 10:08:55
Original
1066 people have browsed it

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



Username:

Password:



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.

www.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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!