Home  >  Article  >  Backend Development  >  PHP user authentication PHP database-based user authentication code

PHP user authentication PHP database-based user authentication code

WBOY
WBOYOriginal
2016-07-25 08:56:241151browse
Share a PHP user authentication code, which performs user authentication based on the information saved in the database. Friends in need can refer to it.

An example code of php implementing user authentication, please refer to it.

Code:

<?php
/**
* 基于数据库的浏览器认证
* edit by bbs.it-home.org
*/

   function authenticate_user() {
      header('WWW-Authenticate: Basic realm="Secret Stash"');
      header("HTTP/1.0 401 Unauthorized");
      exit;
   }

   if (! isset($_SERVER['PHP_AUTH_USER'])) {
      authenticate_user();
   } else {
      mysql_pconnect("localhost","authenticator","secret")
      or die("连接数据库服务器失败!");
      mysql_select_db("java2s") or die("未能连接数据库!");

      $query = "SELECT username, pswd FROM user WHERE
      username='$_SERVER[PHP_AUTH_USER]' AND
      pswd=MD5('$_SERVER[PHP_AUTH_PW]')";

      $result = mysql_query($query);

      // 如果什么都没找到,则继续显示密码输入提示框
      if (mysql_num_rows($result) == 0) {
         authenticate_user();
      }
   }
?>


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