Heim  >  Artikel  >  php教程  >  基于文件的网页身份认证

基于文件的网页身份认证

PHP中文网
PHP中文网Original
2016-05-25 17:16:031203Durchsuche

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

                   

                   

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn