Using session in php to prevent users from logging on to the background illegally

Source: Internet
Author: User
This article describes how to use a session in php to prevent unauthorized user logon to the background. it analyzes in detail the principles and implementation skills of the session to prevent illegal user logon to the background, for more information about how to use session in php to prevent users from logging on to the background illegally, see the following example. Share it with you for your reference. The details are as follows:

Generally, when we log on to the website background, the server will save the logon information to the session file and read the session file to determine whether background operations can be performed.

For example, if admin. php is our background operation page. if the session is not enabled, users can access this page even if they are not logged on, you need to use session to prevent users from logging on to this page illegally. The code for the three files is as follows:

Logon page: login. php

The code is as follows:

User logon page

<? Php
If (! Empty ($ _ GET ['errno']) {
If ($ _ GET ['errno'] = 1 ){
Echo "incorrect user name or password ";
} Else if ($ _ GET ['errno'] = 2 ){
Echo "enter your username and password ";
} Else if ($ _ GET ['errno'] = 3 ){
Echo "illegal access, please enter the user name and password ";
}
}
?>

Log on to the information processing page: loginProcess. php

The code is as follows:

<? Php
// Here we mainly talk about session. concerning login information verification, the database is not involved.
// Receive logon information and save the session
If (! Empty ($ _ POST ['sub']) {
If ($ _ POST ['username'] = "admin" & $ _ POST ['pwd'] = "admin "){
Echo "logon successful ";
Session_start (); // enable session
$ _ SESSION ['username'] =$ _ POST ['username']; // Save the login name to the 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

The code is as follows:

<? Php
Session_start ();
If (empty ($ _ SESSION ['username']) {
Header ("Location: login. php? Errno = 3 ");
Exit ();
}
Echo "you are the administrator, and you now have background management permissions ";
?>

I hope this article will help you with php programming.

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.