php网页安全认证的实例详解
不基于数据库:
<?php
//unset($_server['php_auth_user']);
$strauthuser= $_server['php_auth_user'];
$strauthpass= $_server['php_auth_pw'];
if (! ($strauthuser == "a" && $strauthpass == "a")) {
header('www-authenticate: basic realm="wly"');
header('http/1.0 401 unauthorized');
echo "用户验证!!";
exit;
} else {
echo "验证通过";
header("location:http://www.baidu.com");
//unset($_server['php_auth_user']);
}
?>
基于数据库:
<?php
function authenticate_user() {
header('www-authenticate: basic realm="secret stash"');
header("http/1.0 401 unauthorized");
exit;
}
if (! isset($_server['php_auth_user'])) {
authenticate_user();
} else {
mysql_pconnect("localhost","authenticator","secret") or die("can't connect to database server!");
mysql_select_db("java2s") or die("can't select authentication database!");
$query = "select username, pswd from user where username='$_server[php_auth_user]' and pswd=md5('$_server[php_auth_pw]')";
$result = mysql_query($query);
// if nothing was found, reprompt the user for the login information.
if (mysql_num_rows($result) == 0) {
authenticate_user();
}
}
?>
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!