Heim  >  Artikel  >  Backend-Entwicklung  >  php 登录验证的代码(基于文件保存的密码)

php 登录验证的代码(基于文件保存的密码)

WBOY
WBOYOriginal
2016-07-25 08:56:49787Durchsuche
本文介绍下,用php实现的基于文件保存的密码,以进行登录验证的一段代码,有需要的朋友参考下。

php实现用户登录的验证,代码:

<?php
/**
* @ 用户登录验证
* @ by bbs.it-home.org
*/
   $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
<!-- 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