If you want to implement password protection based on each script, you can create a basic authentication mechanism by combining the header () function with the $ PHP_AUTH_USER and $ PHP_AUTH_PW global variables. Server-based authentication requests/Requests
If you want to implement password protection based on each script, you can create a basic authentication mechanism by combining the header () function with the $ PHP_AUTH_USER and $ PHP_AUTH_PW global variables. Generally, the server-based authentication request/response process is as follows:
1. the user requests a file from a Web server. If the file is in a protected area, the server adds a 401 (invalid user) string to the response data header as a response.
2. the user name/password dialog box is displayed after the browser sees the response.
3. enter the user name and password in the dialog box, and click "OK" to send the information back to the server for authentication.
4. if the user name and password are valid, the protected file will be displayed to the user. This validation will continue to be valid for the duration of the verified user in the protected area.
A simple PHP script can send an appropriate HTTP header to actively display the user name/password dialog box on the client screen to simulate the HTTP authentication request/response system. PHP stores the information in the user input dialog box in the $ PHP_AUTH_USER and $ PHP_AUTH_PW variables. By applying these variables, you can store a list that does not match the real user name/password test to a text file, database, or any location you want to see.
Note: The global variables $ PHP_AUTH_USER, $ PHP_AUTH_PW, and $ PHP_AUTH_TYPE are valid only when PHP is installed as a module. For example, if you are applying the CGI version of PHP, you can only apply the htaccess-based authentication or database-based authentication method. you can enter the user name and password through the HTML form, and then check the PHP validity.
This example shows the confirmation of the two hardware encoding values, no matter where the user name and password are stored, which is theoretically identical.
/* Check the values of $ PHP_AUTH_USER and $ PHP_AUTH_PW */
If ((! Isset ($ PHP_AUTH_USER) | (! Isset ($ PHP_AUTH_PW ))){
/* Null value: send the data header that generates the display text box */
Header ('www-Authenticate: Basic realm = 'My Private buffer '');
Header ('http/1.0 401 unauthorized ');
Echo 'authorization Required .';
Exit;
} Else if (isset ($ PHP_AUTH_USER) & (isset ($ PHP_AUTH_PW ))){
/* Check whether the variable value exists */
If ($ PHP_AUTH_USER! = 'Validname') | ($ PHP_AUTH_PW! = 'Goodpassword ')){
/* User name input error or password input error. send the data header that displays the text box */
Header ('www-Authenticate: Basic realm = 'My Private buffer '');
Header ('http/1.0 401 unauthorized ');
Echo 'authorization Required .';
Exit;
} Else if ($ PHP_AUTH_USER = 'validname') | ($ PHP_AUTH_PW = 'goodpassword ')){
/* The username and password are both accurate and the success information is output */