Using PHP to simulate HTTP authentication _php

Source: Internet
Author: User
Keywords authentication impersonation use user password php_auth_pw ph
Tags http authentication password protection
If you want to implement password protection on a per-script basis, you can create a basic authentication mechanism by combining the header () function with the $php_auth_user, $PHP _AUTH_PW global variables. Typically the server-based authentication request/response process is as follows:



1. The user requests a file from a Web server. If the file is within a protected area, the server responds with a 401 (illegal user) string inside the header of the response data.

2. When the browser sees the response, the User name/password dialog box pops up.

3. The user enters the user name and password in the dialog box, and then clicks 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 confirmation will continue to be valid for the confirmed user's time in the protected area.

A simple PHP script can simulate the HTTP authentication request/Response system by sending the appropriate HTTP header to automatically display the Username/Password dialog box on the client screen. PHP stores the User Input dialog box information in the $php_auth_user and $PHP_AUTH_PW variables. By using these variables, you can store a list of non-compliant user name/password checks in a text file, database, or anywhere you wish.

Note: $PHP _auth_user, $PHP _AUTH_PW, and $php_auth_type global variables are only valid if PHP is installed as a module. If you are using a CGI version of PHP, you will be limited to using htaccess-based authentication or database-based authentication, and let the user enter the user name and password via an HTML form, and then let PHP do the check for validity.

This example shows a confirmation check of two hardware encoded values, which is theoretically identical regardless of where the user name and password are stored.


/* Check the values of variables $PHP _auth_user and $PHP_AUTH_PW */

if ((!isset ($PHP _auth_user)) | | (!isset ($PHP _AUTH_PW))) {

/* Null value: Send the data header that produces the Display text box */

Header (' Www-authenticate:basic realm= ' My Private Stuff ');

Header (' http/1.0 401 Unauthorized ');

Echo ' Authorization Required. ';

Exit

} else if ((Isset ($PHP _auth_user)) && (Isset ($PHP _auth_pw)) {

/* Variable value exists, check that it is correct */

if ($PHP _auth_user! = "Validname") | | ($PHP _AUTH_PW! = "Goodpassword")) {

/* User name input error or password input error, send the data header that produces the Display text box */

Header (' Www-authenticate:basic realm= ' My Private Stuff ');

Header (' http/1.0 401 Unauthorized ');

Echo ' Authorization Required. ';

Exit

} else if (($PHP _auth_user = = "Validname") | | ($PHP _AUTH_PW = = "Goodpassword")) {

/* User name and password are correct, output success information */

echo "

You ' re authorized!

";

}

}

?>

It's important to note that when you are using file-based protection, this approach does not provide a full range of security for your catalog. This is obvious to most people, but if your brain creates a connection between the popup dialog and the protection of the given directory, you should consider this further.

Julie Meloni is the technical director of I2i Interactive and a strong catalyst for the Linux and open source communities. She has authored a number of books on PHP and other technologies and has become an expert on the long-term contribution of CNET Builder.com.
  • 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.