An example of PHP simulating HTTP authentication code

Source: Internet
Author: User
Tags http authentication
For you to introduce a PHP analog HTTP authentication example, mainly combining the header () function and $php_auth_user, $PHP _AUTH_PW global variable method to create a basic authentication mechanism for everyone to learn the reference.

The code is as follows:

 
      You ' re authorized!

"; } } ? >

Attached, PHP implements basic identity authentication instructions.

You can create a basic authentication mechanism by combining the header () function and 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.

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

Hope that the above demo code, for everyone to understand PHP analog HTTP authentication has some help.

  • Related Article

    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.