In PHP, there are two methods to implement user identity authentication. When designing and maintaining websites, users often need to restrict access to some important files or information. Generally, we can adopt a user identity authentication mechanism built into the Web HTTP protocol. When designing and maintaining websites, users often need to restrict access to some important files or information. Generally, we can adopt a user identity authentication mechanism built into the Web HTTP protocol. When a visitor browses a protected page, the client browser will pop up a dialog window asking the user to enter the user name and password to verify the user's identity to determine whether the user has the right to access the page. The following two methods are used to describe the implementation principle.
1. implement HTTP headers
The header is the string sent by the server before the server sends HTML information to the browser over HTTP. HTTP uses a challenge/response mode to authenticate users attempting to enter the password-protected area. Specifically, when a user sends a request to the Web server for the first time to access a protected area, the challenge process is started, and the server returns a special 401 header, indicating that the user's identity is not verified. After the client browser detects the above response, a dialog box is displayed automatically asking the user to enter the user name and password. After the user completes the input, click "OK". the user's identification information is sent to the server for verification. If the user name and password entered by the user are valid, the WEB server allows the user to enter the protected area and maintain the identity validity throughout the access process. On the contrary, if the user name or password entered by the user cannot be verified, the client browser will pop up the input window asking the user to try again to enter the correct information. The entire process will continue until the user enters the correct information location. you can also set the maximum number of attempts allowed to the user. when the limit is exceeded, the user's access request will be automatically rejected.
In the PHP script, use the function header () to directly send the HTTP header to the browser of the client. in this way, the user name and password input window will pop up automatically on the client to implement our identity authentication function. In PHP, the information entered by the client user is automatically stored in three global variables: $ PHP_AUTH_USER, $ PHP_AUTH_PW, and $ PHP_AUTH_TYPE. With these three variables, we can verify the user identity based on the user account information stored in the data file or database!
However, you must note that the $ PHP_AUTH_USER, $ PHP_AUTH_PW, and $ PHP_AUTH_TYPE variables can be used only in PHP installed in module mode. If you are using CGI-mode PHP, the verification function cannot be implemented. Install PHP modules later in this section.
Next we will use the MySQL database to store the user's identity. We need to extract the username and password of each account from the database to compare with the $ PHP_AUTH_USER and $ PHP_AUTH_PW variables to determine the authenticity of the user.
First, create a database for storing user information in MySQL.
The database name is XinXiKu and the table name is user. The Table definition is as follows:
create table user(ID INT(4) NOT NULL AUTO_INCREMENT,name VARCHAR(8) NOT NULL,password CHAR(8) NOT NULL,PRIMARY KEY(ID)) |
Note:
1. ID is a serial number, which is not zero and increases automatically. it is the primary key;
2. name is the user name and cannot be blank;
3. the password is a user password and cannot be blank;
The following is the user authentication file login. php.
<? PHP // Determine whether the user name is set if (! Isset ($ PHP_AUTH_USER) {header ("WWW-Authenticate: Basic realm =" authentication function ""); header ("HTTP/1.0 401 Unauthorized "); echo "authentication failed. you do not have permission to share network resources! "; Exit () ;}/ * connect to the database */$ db = mysql_connect (" localhost "," root "," "); // select the database mysql_select_db (" XinXiKu ", $ db); // query whether a user exists $ result = mysql_query ("SELECT * FROM user where name = '$ PHP_AUTH_USER' and password = '$ PHP_AUTH_PW' ", $ db); if ($ myrow = mysql_fetch_row ($ result )) {// The following are related operations after successful authentication ...} else {// The authentication fails, prompting the user to re-enter the header ("WWW-Authenticate: Basic realm =" authentication function ""); header ("HTTP/1.0 401 Unauthorized"); echo "authentication failed, you do not have permission to share network resources! "; Exit () ;}?> |
Program description:
In the program, first check whether the variable $ PHP_AUTH_USER has been set. If no authentication is set, the script sends an HTTP 401 error header, telling the client browser that authentication is required. an authentication window is displayed in the client browser, prompt the user to enter the user name and password. after entering the password, connect to the database and check whether the user name and password are correct. if the user name and password are correct, allow logon to perform related operations. if the user name and password are incorrect, the user is required to enter the user name and password.
Function description:
1. isset (): used to determine whether a variable has been assigned a value. Returns true or false based on whether the variable value exists.
2. header (): used to send specific HTTP headers. Note: When using the header () function, you must call this function before any HTML or PHP code that generates the actual output.
3. mysql_connect (): Open the MySQL server connection.
4. mysql_db_query (): Send the query string to the MySQL database.
5. mysql_fetch_row (): return fields in a single column.
II. server verification using Session
For pages that require authentication, it is best to use the Apache server for authentication. However, the interface for Apache server authentication is unfriendly. In addition, PHP in CGI mode and PHP in IIS cannot be verified using the Apache server. In this way, we can use the Session to save the user identity between different pages to achieve the purpose of identity authentication.
At the backend, we also use the above MySQL database to store user information.
First, write a user logon interface named login. php. the code is as follows:
<Form action = "login1.php"> user name: <input type = "text" name = "name" ><br> command: <input type = "text" name = "pass"> <br> <input type = "submit" value = "logon" ><</form> |
The code for login1.php to process the submitted form is as follows:
<? PHP $ db = mysql_connect ("localhost", "root", ""); mysql_select_db ("XinXiKu", $ db ); $ result = mysql_query ("SELECT * FROM user where name = '$ name' and password =' $ pass'", $ db ); if ($ myrow = mysql_fetch_row ($ result) {// registered user session_start (); session_register ("user"); $ user = $ myrow ["user"]; // complete the authentication ...} else {echo "authentication failed. you do not have permission to share network resources! ";}? > |
You can use ** http: // domainname/next. php? User = Username ** to bypass authentication. Therefore, follow-up operations should first check whether the variable is registered: if it is already registered, perform the corresponding operations; otherwise, it is deemed as illegal logon. The related code is as follows:
<? PHPsession_start (); if (! Session_is_registered ("user") {echo "authentication failed, illegal logon! ";} Else {// log on successfully to perform related operations ...}? > |
Appendix: PHP module installation method
1. first download the file: mod_php4-4.0.1-pl2. [If you are not using PHP4, upgrade it now.]
There are three files after unzipping: mod_php4.dll1_mod_php4.conf1_readme.txt
2. copy related files
Copy mod_php4.dll to the modules Directory of the apache installation directory.
Copy mod_php4.conf to the conf Directory of the apache installation directory.
Copy the msvcrt. dll file to the apache installation directory.
3. open the conf/srm. conf file and add a sentence to it.
Include conf/mod_php4.conf
Before doing this, please remove all the settings statements about the CGI mode in your httpd. conf, which is similar to the following section!
ScripAlias/php4/ "C:/php4/"AddType application/x-httpd-php4.phpAddType application/x-httpd-php4.php3AddType application/x-httpd-php4.php4Action application/x-httpd-php4/php4/php.exe |
It's okay to make PHP support more extensions. The configuration file mod_php4.conf already supports three extensions: php, php3, and php4. if you want to support more extensions, you can change the file.
4. test
Use <? Phpinfo ();? > Test. The Server API value is Apache, not CGI, and there is Information about HTTP Headers Information.
Bytes. Generally, we can adopt a user identity authentication mechanism built into the Web HTTP protocol ....