PHP网页安全认证的实例详解
更新时间:2017年09月28日 14:17:24 作者:callie
这篇文章主要介绍了PHP网页安全认证的实例详解的相关资料,这里提供了两种实现方法,一种基于数据库另一种不基于数据库的方法,希望通过本能帮助到大家,需要的朋友可以参考下
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(); } } ?>
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
使用PHPStudy搭建Cloudreve网盘服务的流程步骤
自云存储概念兴起已经有段时间了,各互联网大厂也纷纷加入战局,一时间公有云盘遍地开花,今天我们就为大家介绍,如何使用Cpolar与Cloudreve,在个人Windows电脑上搭建一个强大的PHP云盘系统,需要的朋友可以参考下2024-02-02PHP简单实现图片格式转换(jpg转png,gif转png等)
这篇文章主要介绍了PHP简单实现图片格式转换(jpg转png,gif转png等),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-10-10
最新评论