Maison >développement back-end >tutoriel php >php摘要认证
根据
A1=MD5(
A2=MD5(
最后结果:MD5(MD5(A1):
注意上面一行的MD5(A1)和MD5(A2)是有问题的,浏览器实现为MD5(A1:
$realm='saberyjs';
$password='0011';//这个地方式要查询数据库的,只是为测试方便而已;
if(!isset($_SERVER['PHP_AUTH_DIGEST']))
{
$n> header("www-authenticate:Digest realm ='".$realm."',nonce='".$nonce."',algorithm='md5',qop='auth'");
header("HTTP/1.0 401 Unauthorization Required");
exit();
}
else
{
//echo 'sset';
//echo $_SERVER['PHP_AUTH_DIGEST'];
$result=parse_auth_digest($_SERVER['PHP_AUTH_DIGEST']);
//var_dump($result);
//echo hexdec($result['nc']);
//exit();
if((int)$result['nc']>1)
{
//echo '随机数已经过期';
$n> header("www-authenticate:Digest realm ='".$realm."',nonce='".$nonce."',algorithm='md5',qop='auth'");
header("HTTP/1.0 401 Unauthorization Required");
exit();
}
$A1=md5($result['username'].":".$result['realm'].":".$password);
//$A1=md5($result['username'].':'.$result['realm'].':'.$password).':'.$result['nonce'].':'.$result['cnonce'];
$A2=md5($_SERVER['REQUEST_METHOD'].":".$result['uri']);
$degest=md5($A1.":".$result['nonce'].':'.$result['nc'].':'.$result['cnonce'].':'.$result['qop'].':'.$A2);
// var_dump($degest);
if($degest!=$result['response'])
{
echo '验证失败';
var_dump($result);
}
else
{
echo '成功';
}
}
function parse_auth_digest($auth_digest)
{
$pairs=array();
preg_match_all('/(\w+)=[\'"]?([\/a-z0-9A-Z.]+)[\'"]?/i',$auth_digest,$result,PREG_SET_ORDER);
foreach($result as $value)
$pairs[$value[1]]=$value[2];
//var_dump($pairs);
return $pairs;
}
?>
以上就介绍了php摘要认证,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。