PHP Apache Php_auth_user User Login method

Source: Internet
Author: User

PHP's HTTP authentication mechanism works only when PHP is running in Apache mode, so this feature does not apply to CGI versions. In the Apache module's PHP script, you can use the header () function to send "authentication Required" information to the client browser to eject a username/password input window. When the user enters the username and password, the PHP script containing the URL will be invoked again with the predefined variable php_auth_user, PHP_AUTH_PW, and Auth_type, which are set to the username, password, and authentication type respectively. Predefined variables are saved in an array of $_server or $HTTP _server_vars. The system only supports "basic" authentication

<?php Tutorial
$authorized = FALSE;

if (Isset ($_server[' Php_auth_user ')) && isset ($_server[' PHP_AUTH_PW ')) {
$authFile = File ("./password.txt");

      foreach ($authFile as $login) {
          List ($username, $password) = Explode (":", $login);
         $password = Trim ($password);
         if (($username = = $_server[' Php_auth_user ']) && ($ Password = = MD5 ($_server[' PHP_AUTH_PW '))) {
             $authorized = TRUE;
            break;
        }
     }
  }

If not authorized, display authentication prompt or 401 error
if (! $authorized) {
Header (' Www-authenticate:basic realm= "Secret Stash");
Header (' http/1.0 401 Unauthorized ');
Print (' You must provide the proper credentials! ');
Exit
}

?>


<!--Password.txt
Joe:60d99e58d66a5e0f4f89ec3ddd1d9a80

-->

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.