linuxnotes
发布:thebaby   来源:net     【大 中 小】
本文介绍下,在php中,进行用户登录验证的例子,这个是基于WWW-Authenticate登录验证的实例,有需要的朋友参考下吧。
大家是否遇到过,打开一个网页时,会弹出一个浏览器的提示框,让你输入用户名与密码呢?
本节分享的这段代码,就是实现这样的登录验证,快来看看吧。
代码:  
01 <?php
02 /**  

03 * WWW-Authenticate 登录验证  

04 * edit: www.jbxue.com  

05 
*/ 

06    function authenticate_user() {  

07       header(\'WWW-Authenticate: Basic realm="Secret Stash"\');  

08       header("HTTP/1.0 401 Unauthorized");  

09       exit;  

10    }  

11    

12    if (! isset($_SERVER[\'PHP_AUTH_USER\'])) {  

13       authenticate_user();  

14    } else {  

15       mysql_pconnect("localhost","authenticator","secret") or die("无法连接数据库服务器。");  

16       mysql_select_db("navioo") or die("无法连接数据库。");  

17    

18       $query = "SELECT username, pswd FROM user WHERE username=\'$_SERVER[PHP_AUTH_USER]\' AND pswd=MD5(\'$_SERVER[PHP_AUTH_PW]\')";  

19    

20       $result = mysql_query($query);  

21    

22       //验证失败,继续返回登录窗口  

23       if (mysql_num_rows($result) == 0) {  

24          authenticate_user();  

25       }  

26    }  

27 ?> 
本文原始链接:http://www.jbxue.com/article/11342.htmlhttp://www.jbxue.com/article/11342.html

分类:

技术点:

相关文章: