首页 > 代码库 > HTTP认证用户名密码 php

HTTP认证用户名密码 php

// 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‘].‘<br/>‘;
echo ‘Password:‘.$_SERVER[‘PHP_AUTH_PW‘].‘<br/>‘;


HTTP认证用户名密码 php