国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php使用Header函數,PHP_AUTH_PW和PHP_AUTH_USER做用戶驗證

2024-05-04 21:50:50
字體:
來源:轉載
供稿:網友

本文實例講述了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. //Vevb.com 
  67.  
  68. $name=mysql_result($query,0,"name"); 
  69.  
  70. $email=mysql_result($query,0,"email"); 
  71.  
  72. mysql_free_result($query); 
  73.  
  74.  
  75. ?> 

另外一個參考的例程:

  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. //connect to db 
  15.  
  16. include 'db_connect.php'
  17.  
  18. //SQL query to find if this entered username/password is in the db 
  19.  
  20. $sql = "SELECT * FROM healthed_workshop_admin WHERE 
  21.  
  22. user = '$PHP_AUTH_USER' AND 
  23.  
  24. pass = '$PHP_AUTH_PW'"; 
  25.  
  26. //put the SQL command and SQL instructions into variable 
  27.  
  28. $result = mysql_query($sqlor die('Unable to connect.'); 
  29.  
  30. //get number or rows in command; if more than 0, row is found 
  31.  
  32. $num_matches = mysql_num_rows($result); 
  33.  
  34. if ($num_matches !=0) 
  35.  
  36.  
  37. //matching row found authenticates user 
  38.  
  39. $auth = true; 
  40.  
  41.  
  42.  
  43. if (!$auth
  44.  
  45.  
  46. header('WWW-Authenticate: Basic realm="Health Ed Presentation Admin"'); 
  47.  
  48. header('HTTP/1.0 401 Unauthorized'); 
  49.  
  50. echo 'You must enter a valid username & password.'
  51.  
  52. exit
  53.  
  54.  
  55. else 
  56.  
  57.  
  58. echo 'Success!'
  59.  
  60.  
  61. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浦县| 六安市| 湖北省| 华安县| 凤凰县| 堆龙德庆县| 罗平县| 青岛市| 河北省| 仁布县| 本溪市| 溧水县| 滨海县| 兰州市| 聊城市| 鄂托克前旗| 玉树县| 大洼县| 神木县| 孝感市| 鸡东县| 宁远县| 大名县| 铜山县| 景德镇市| 泸水县| 普洱| 忻州市| 长春市| 邓州市| 诏安县| 湖口县| 卢湾区| 通化市| 荃湾区| 湘潭县| 定襄县| 拜泉县| 米林县| 紫阳县| 拉萨市|