[Php] after logging on to the system and logging on to the system with output viewer information,
After learning about how to operate sessions
We should know how to build a system.
The idea of this system is exactly the same as that of ASP login system in the previous article [asp] login system for Access database (click to open the link,
Only the programming language changes from asp to php, and the database changes from Access to mysql.
I. basic objectives
First, the following user information table exists in mysql:
There is a logon form on the page. you need to enter the user name, password, and other information.
If the user name entered by the user does not exist in the user information table, the "check for this person" dialog box will pop up and return to this page.
If the password entered by the user is incorrect, the "incorrect password" dialog box is displayed, and the system returns to this page.
If the login information entered by the user is correct, the "login successful" page is displayed, and the current IP address, the language environment of the system used, and browser information are output.
The "login successful" page is protected, and the browser cannot bypass the password input page by entering the address in the browser, directly accessing the "login successful" page
II. basic ideas
The page on which the user enters the user name and password uses a static page, as long as the form points to the logon judgment page login. php.
Welcome. php is the page on which the user successfully logs on,
Exit. php is used to destroy the session.
Session is a global browser variable used to store user names and passwords.
III. Production process
(1)login.html
There is nothing to say, just a login form, see the following code:
Logon page
(2) exit. php
Describe the sessionpage, and after describe the session, roll the page back to login.html
It should be noted that, once session is used in php, session_start (); system function should be used in the first line of all code; otherwise, an error may occur.
Logout page
Script window. location. href = "login.html"; script
(3) login. php
The logon judgment page is still a classic logon field theory,
First, check whether the username and password passed by login.html are in the user information table,
If no, logon fails. If yes, you can determine whether the user-entered password is the dbpassword corresponding to this username in the database,
If yes, the logon is successful, and the username is stored in the session and passed to the logon success page. Otherwise, the logon fails.
This page also uses the built-in system function is_null to determine whether the query result is null,
If the database query result is empty, no value is assigned to the newly defined dbusername. this dbusername is still empty.
At the same time, if the logon is successful, you must use mt_rand ~ 100000 generates a random number code to protect the logon success page.
After making all the judgments, remember to add a statement to close the database.
Logging in ......
Script alert ("no such person found! "); Window. location. href =" login.html "; script
Script alert ("incorrect password! "); Window. location. href =" login.html "; script
Script window. location. href = "welcome. php"; script
(4) welcome. php
On the successful login page of welcome. php, you must first use isset to determine whether the code passed by login. php exists,
Here is the difference between isset and is_null. isset is used to determine whether this variable exists. is_null is used to determine whether the variable is null,
If there is no code for this session, it indicates that the user has logged on through an abnormal way, and immediately throw it to exit. php.
If the logon is normal, the user information is output through each small object in the server object.
Welcome to log on
Welcome to log on!
Your IP address:
Your system language:
Your browser is:
Log out
Script alert ("please log on normally! "); Window. location. href =" exit. php "; script
So far, the entire login system has been developed.