PHP Apache Php_auth_user User Login Method _php Tutorial

Source: Internet
Author: User
Tags http authentication
PHP's HTTP authentication mechanism only works when PHP is running as an Apache module, so this feature is not available for CGI versions. In the Apache module PHP script, you can use the header () function to send the "authentication Required" message to the client browser, which pops up a username/Password Entry window. When the user enters the user name and password, the PHP script containing the URL will be called again with the predefined variables php_auth_user, PHP_AUTH_PW, and Auth_type, which are set to the user name, password, and authentication type, respectively. The predefined variables are saved in the $_server or $HTTP _server_vars array. The system only supports "basic" authentication

$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
}

?>


http://www.bkjia.com/PHPjc/629712.html www.bkjia.com true http://www.bkjia.com/PHPjc/629712.html techarticle PHP's HTTP authentication mechanism only works when PHP is running as an Apache module, so this feature is not available for CGI versions. In the Apache module PHP script, you can use the header () function ...

  • 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.