Home >php教程 >PHP源码 >基于文件的网页身份认证

基于文件的网页身份认证

PHP中文网
PHP中文网Original
2016-05-25 17:16:031249browse

php代码  

<?php
   $authorized = FALSE;

   if (isset($_SERVER[&#39;PHP_AUTH_USER&#39;]) && isset($_SERVER[&#39;PHP_AUTH_PW&#39;])) {
      $authFile = file("./password.txt");

      foreach ($authFile as $login) {
         list($username, $password) = explode(":", $login);
         $password = trim($password);
         if (($username == $_SERVER[&#39;PHP_AUTH_USER&#39;]) && ($password == md5($_SERVER[&#39;PHP_AUTH_PW&#39;]))) {
            $authorized = TRUE;
            break;
         }
      }
   }

   // If not authorized, display authentication prompt or 401 error
   if (! $authorized) {
      header(&#39;WWW-Authenticate: Basic Realm="Secret Stash"&#39;);
      header(&#39;HTTP/1.0 401 Unauthorized&#39;);
      print(&#39;You must provide the proper credentials!&#39;);
      exit;
   }

?>


<!-- 
password.txt
joe:60d99e58d66a5e0f4f89ec3ddd1d9a80
-->

                   

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn