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.