1. 关于文件身份验证方式
解析建立的.htpasswd文件存放的登录账号和密码
提取前台发来的http的验证登录账号和密码
$_SERVER['PHP_AUTH_USER']和 $_SERVER['PHP_AUTH_PW']
分别比对 代码 和关于数据库方式大概一致
代码地址
https://github.com/956077081/PHP_demo/blob/master/HttpLogin.php
2. 关于基于数据库的方式进行文件比对
<?php
//验证Http的两个参数
//$_SERVER['PHP_AUTH_USER']
//$_SERVER['PHP_AUTH_PW'];
//echo $_SERVER['PHP_AUTH_USER']."\r\n";
//echo $_SERVER['PHP_AUTH_PW'];
function authenticate_user ()
{
header("WWW-Authenticate: Basic realm ='Project'");
header("HTTP/1.1 401 unauthorized");
}
$user = $_SERVER['PHP_AUTH_USER'];
$passwd = $_SERVER['PHP_AUTH_PW'];
if( !isset($user) ||!isset($passwd)){
authenticate_user();
}else{
$db = new mysqli("localhost", "root", "123456", "httpauth");
$stm = $db->prepare("select name ,passwd from auth where name=? and passwd=?");
$stm->bind_param("ss",$user,$passwd);
$stm->execute();
$stm->store_result();
if ( $stm->num_rows == 0 ){
authenticate_user();
}else{
echo "you are sucessful to login !";
}
}
对应的数据库(httpauth) 明文的验证方式也可以自行做数据库 与 php原数据 加密比对
use httpauth;
create table autht(
id int(10) UNSIGNED not NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL DEFAULT '' ,
passwd VARCHAR(50) not NULL DEFAULT '',
harsh VARCHAR(50) DEFAULT '',
PRIMARY KEY(id,name),
INDEX `asd` (name)
) ENGINE = INNODB , DEFAULT CHARSET = UTF8 COMMENT="HTTP登录验证"