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

首頁 > 語言 > PHP > 正文

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

2024-05-04 23:45:26
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了php使用Header函數,PHP_AUTH_PW和PHP_AUTH_USER做用戶驗證的方法,結合實例形式分析了PHP使用Header函數調用登錄驗證及PHP_AUTH_PW和PHP_AUTH_USER進行驗證處理的相關技巧,需要的朋友可以參考下
 

本文實例講述了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']

獲取到用戶提交上來的用戶名和密碼之后,要怎樣處理邏輯就跟我們一般的程序處理沒有什么區別了。下面提供兩個例程供參考:

<?phpif(!isset($PHP_AUTH_USER)) {Header("WWW-authenticate: basic realm="XXX"");Header("HTTP/1.0 401 Unauthorized");$title="Login Instructions";?><blockquote>In order to enter this section of the web site, you must be an XXXsubscriber. If you are a subscriber and you are having trouble loggingin,please contact <a href="mailto:support@xxx.com">support@xxx.com</a>.</blockquote><?phpexit;} else {mysql_pconnect("localhost","nobody","") or die("Unable to connect to SQL server");mysql_select_db("xxx") or die("Unable to select database");$user_id=strtolower($PHP_AUTH_USER);$password=$PHP_AUTH_PW;$query = mysql_query("select * from users where user_id='$user_id' and password='$password'");if(!mysql_num_rows($query)) {Header("WWW-authenticate: basic realm="XXX"");Header("HTTP/1.0 401 Unauthorized");$title="Login Instructions";?><blockquote>In order to enter this section of the web site, you must be an XXXsubscriber. If you are a subscriber and you are having troublelogging in,please contact <a href="mailto:support@xxx.com">support@xxx.com</a>.</blockquote><?phpexit;}$name=mysql_result($query,0,"name");$email=mysql_result($query,0,"email");mysql_free_result($query);}?>

另外一個參考的例程:

<?php//assume user is not authenticated$auth = false;$user = $_SERVER['PHP_AUTH_USER'];$pass = $_SERVER['PHP_AUTH_PW'];if ( isset($user) && isset($pass) ){//connect to dbinclude 'db_connect.php';//SQL query to find if this entered username/password is in the db$sql = "SELECT * FROM healthed_workshop_admin WHEREuser = '$PHP_AUTH_USER' ANDpass = '$PHP_AUTH_PW'";//put the SQL command and SQL instructions into variable$result = mysql_query($sql) or die('Unable to connect.');//get number or rows in command; if more than 0, row is found$num_matches = mysql_num_rows($result);if ($num_matches !=0){//matching row found authenticates user$auth = true;}}if (!$auth){header('WWW-Authenticate: Basic realm="Health Ed Presentation Admin"');header('HTTP/1.0 401 Unauthorized');echo 'You must enter a valid username & password.';exit;}else{echo 'Success!';}?>
 


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 南京市| 景东| 长春市| 峨边| 武陟县| 桐庐县| 乌兰察布市| 于都县| 大方县| 道孚县| 锦屏县| 牙克石市| 湘西| 贡嘎县| 都兰县| 宾川县| 景宁| 合山市| 香河县| 会宁县| 长兴县| 蓬安县| 盐源县| 阿坝县| 治多县| 德保县| 文昌市| 华安县| 高清| 班戈县| 神木县| 健康| 楚雄市| 平顶山市| 巴青县| 正宁县| 泗洪县| 钦州市| 淅川县| 南召县| 肇源县|