// header('HTTP/1.1 401 Authorization Required');
// header('WWW-Authenticate: Basic realm="PHP Secured"');
//用户名和口令列表
$users = array(
'aaa' => 'aaa',
'bbb' => 'bbb'
);
//检验是否有用户名
if(!isset($_SERVER['PHP_AUTH_USER']))
{
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="PHP Secured"');
exit('This page requires authentication');
}
//检验是否有该用户
if(!isset($users[$_SERVER['PHP_AUTH_USER']]))
{
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="PHP Secured"');
exit('Unauthorized!');
}
//检验口令是否正确
if($users[$_SERVER['PHP_AUTH_USER']] != $_SERVER['PHP_AUTH_PW'])
{
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="PHP Secured"');
exit('Unauthorized!');
}
//验证通过
echo 'Username:'.$_SERVER['PHP_AUTH_USER'].'
';
echo 'Password:'.$_SERVER['PHP_AUTH_PW'].'
';
网站栏目:HTTP认证用户名密码php
当前路径:
http://bzwzjz.com/article/ppcjdh.html