Home  >  Article  >  Backend Development  >  用 PHP 开展 HTTP 认证

用 PHP 开展 HTTP 认证

WBOY
WBOYOriginal
2016-06-13 13:20:12819browse

用 PHP 进行 HTTP 认证

有时候项目上线前,需要部分人可以访问到(知道用户名,密码的)。

?

备注:php文档中'PHP_AUTH_USER','PHP_AUTH_PW'有注明是当 PHP 运行在 Apache 或 IIS(PHP 5 是 ISAPI)模块方式下,所以此方法只适用php为apache模块运行方式(未验证)。

?

?

<?php /**
 * @author: xiaoshengeer@gmail.com
 */
error_reporting(E_ALL);
ini_set("display_errors", true);

/**
 * Authenticate
 */
function authenticate($adminName, $adminPassword)
{
    if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
        $_SERVER['PHP_AUTH_USER'] != $adminName || $_SERVER['PHP_AUTH_PW'] != $adminPassword){
            Header("WWW-Authenticate: Basic realm=\"please login\"");
			Header("HTTP/1.0 401 Unauthorized");
			
			echo "<html>Wrong UserName or Password!
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