当前位置:首页 > PHP教程 > php高级应用 > 列表

php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证

发布:smiling 来源: PHP粉丝网  添加日期:2019-08-12 11:34:35 浏览: 评论:0 

本文实例讲述了php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证的方法。分享给大家供大家参考,具体如下:

在php中,可以使用Header函数做一些有趣的事情,用户验证就是其中一个很有意思的功能。具体用法:

Header("WWW-Authenticate: Basic realm="USER LOGIN"");

Header("HTTP/1.0 401 Unauthorized");

在页首设计这两个Header函数,页面在载入前会出现一个登录框,要求输入用户名和密码。习惯了在页面登录的我们,是否觉得这样的登录很原始,又很新奇呢?

为了获取从这个对话框中传来的用户名和密码,需要用到php提供的两个特殊变量$PHP_AUTH_USER和$PHP_AUTH_PW,要这样使用这两个特殊变量好像需要在php.ini中设置相关的选项,不然就只能像下面这样引用:

$_SERVER['PHP_AUTH_USER']

$_SERVER['PHP_AUTH_PW']

获取到用户提交上来的用户名和密码之后,要怎样处理逻辑就跟我们一般的程序处理没有什么区别了。下面提供两个例程供参考:

  1. <?php 
  2.  
  3. if(!isset($PHP_AUTH_USER)) { 
  4.  
  5. Header("WWW-authenticate: basic realm="XXX""); 
  6.  
  7. Header("HTTP/1.0 401 Unauthorized"); 
  8.  
  9. $title="Login Instructions"; 
  10.  
  11. ?> 
  12.  
  13. <blockquote> 
  14.  
  15. In order to enter this section of the web site, you must be an XXX 
  16.  
  17. subscriber. If you are a subscriber and you are having trouble logging 
  18.  
  19. in, 
  20.  
  21. please contact <a href="mailto:support@xxx.com">support@xxx.com</a>. 
  22.  
  23. </blockquote> 
  24.  
  25. <?php 
  26.  
  27. exit; 
  28.  
  29. } else { 
  30.  
  31. mysql_pconnect("localhost","nobody","") or die("Unable to connect to SQL server"); 
  32.  
  33. mysql_select_db("xxx") or die("Unable to select database"); 
  34.  
  35. $user_id=strtolower($PHP_AUTH_USER); 
  36.  
  37. $password=$PHP_AUTH_PW; 
  38.  
  39. $query = mysql_query("select * from users where user_id='$user_id' and password='$password'"); 
  40.  
  41. if(!mysql_num_rows($query)) { 
  42.  
  43. Header("WWW-authenticate: basic realm="XXX""); 
  44.  
  45. Header("HTTP/1.0 401 Unauthorized"); 
  46.  
  47. $title="Login Instructions"; 
  48.  
  49. ?> 
  50.  
  51. <blockquote> 
  52.  
  53. In order to enter this section of the web site, you must be an XXX 
  54.  
  55. subscriber. If you are a subscriber and you are having trouble 
  56.  
  57. logging in, 
  58.  
  59. please contact <a href="mailto:support@xxx.com">support@xxx.com</a>. 
  60.  
  61. </blockquote> 
  62.  
  63. <?php 
  64.  
  65. exit; 
  66. //phpfensi.com 
  67. } 
  68.  
  69. $name=mysql_result($query,0,"name"); 
  70.  
  71. $email=mysql_result($query,0,"email"); 
  72.  
  73. mysql_free_result($query); 
  74.  
  75. } 
  76.  
  77. ?> 

另外一个参考的例程:

  1. <?php 
  2.  
  3. //assume user is not authenticated 
  4.  
  5. $auth = false; 
  6.  
  7. $user = $_SERVER['PHP_AUTH_USER']; 
  8.  
  9. $pass = $_SERVER['PHP_AUTH_PW']; 
  10.  
  11. if ( isset($user) && isset($pass) ) 
  12.  
  13. { 
  14.  
  15. //connect to db 
  16.  
  17. include 'db_connect.php'; 
  18.  
  19. //SQL query to find if this entered username/password is in the db 
  20.  
  21. $sql = "SELECT * FROM healthed_workshop_admin WHERE 
  22.  
  23. user = '$PHP_AUTH_USER' AND 
  24.  
  25. pass = '$PHP_AUTH_PW'"; 
  26.  
  27. //put the SQL command and SQL instructions into variable 
  28.  
  29. $result = mysql_query($sql) or die('Unable to connect.'); 
  30.  
  31. //get number or rows in command; if more than 0, row is found 
  32.  
  33. $num_matches = mysql_num_rows($result); 
  34.  
  35. if ($num_matches !=0) 
  36.  
  37. { 
  38.  
  39. //matching row found authenticates user 
  40.  
  41. $auth = true; 
  42.  
  43. } 
  44.  
  45. } 
  46.  
  47. if (!$auth) 
  48.  
  49. { 
  50.  
  51. header('WWW-Authenticate: Basic realm="Health Ed Presentation Admin"'); 
  52.  
  53. header('HTTP/1.0 401 Unauthorized'); 
  54.  
  55. echo 'You must enter a valid username & password.'; 
  56.  
  57. exit; 
  58.  
  59. } 
  60.  
  61. else 
  62.  
  63. { 
  64.  
  65. echo 'Success!'; 
  66.  
  67. } 
  68.  
  69. ?> 

Tags: Header函数 PHP_AUTH_PW

分享到: