How to use session in PHP to prevent users from illegally logging in backstage _php tips

Source: Internet
Author: User

This example describes the method used in PHP to prevent users from illegally logging in to the background. Share to everyone for your reference. Specifically as follows:

In general, when we log in to the background of the website, the server will save the login information to the session file, and read the session file to determine whether the background operation can be done.

Take the following example, if admin.php is our background operation page, if the session is not enabled, then, even if there is no login, users can access the page, this time, you need to use the session to prevent users from illegally logged into this page. Here is the code for three files

Login page: login.php

Copy Code code as follows:
<form action= "loginprocess.php" method= "POST" >
User name: <input type= "text" name= "username" ><br/>
Password: <input type= "password" name= "pwd" ><br/>
<input type= "Submit" Name= "sub" value= "Login Backstage" >
</form>
<?php
if (!empty ($_get[' errno ')) {
if ($_get[' errno ']==1) {
echo "User name or password error";
}else if ($_get[' errno ']==2) {
echo "Please enter username password";
}else if ($_get[' errno ']==3) {
echo "Illegal access, please enter user name and password";
}
}
?>

Login Information Processing page: loginprocess.php

Copy Code code as follows:
<?php
This is mainly about the session, on the verification of login information, it does not involve the database
Receive login information, save session
if (!empty ($_post[' Sub ')) {
if ($_post[' username ']== "admin" && $_post[' pwd ']== ' admin ') {
echo "Login succeeded";
Session_Start ()//Open session
$_session[' username '] = $_post[' username '];//Save Login to session
Header ("Location:admin.php");
Exit ();
}else{
Header ("Location:login.php?errno=1");
Exit ();
}
}else{
Header ("location:login.php?errno=2");
Exit ();
}
?>

Background file: admin.php

Copy Code code as follows:
<?php
Session_Start ();
if (Empty ($_session[' username ')) {
Header ("location:login.php?errno=3");
Exit ();
}
echo "You are the administrator, you now have background Management Authority";
?>

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.