php使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶(hù)驗(yàn)證
本文實(shí)例講述了php使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶(hù)驗(yàn)證的方法。分享給大家供大家參考,具體如下:
在php中,可以使用Header函數(shù)做一些有趣的事情,用戶(hù)驗(yàn)證就是其中一個(gè)很有意思的功能。具體用法:
Header("WWW-Authenticate: Basic realm="USER LOGIN""); Header("HTTP/1.0 401 Unauthorized");
在頁(yè)首設(shè)計(jì)這兩個(gè)Header函數(shù),頁(yè)面在載入前會(huì)出現(xiàn)一個(gè)登錄框,要求輸入用戶(hù)名和密碼。習(xí)慣了在頁(yè)面登錄的我們,是否覺(jué)得這樣的登錄很原始,又很新奇呢?
為了獲取從這個(gè)對(duì)話框中傳來(lái)的用戶(hù)名和密碼,需要用到php提供的兩個(gè)特殊變量$PHP_AUTH_USER和$PHP_AUTH_PW,要這樣使用這兩個(gè)特殊變量好像需要在php.ini中設(shè)置相關(guān)的選項(xiàng),不然就只能像下面這樣引用:
$_SERVER['PHP_AUTH_USER'] $_SERVER['PHP_AUTH_PW']
獲取到用戶(hù)提交上來(lái)的用戶(hù)名和密碼之后,要怎樣處理邏輯就跟我們一般的程序處理沒(méi)有什么區(qū)別了。下面提供兩個(gè)例程供參考:
<?php if(!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 XXX subscriber. If you are a subscriber and you are having trouble logging in, please contact <a href="mailto:support@xxx.com">support@xxx.com</a>. </blockquote> <?php exit; } 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 XXX subscriber. If you are a subscriber and you are having trouble logging in, please contact <a href="mailto:support@xxx.com">support@xxx.com</a>. </blockquote> <?php exit; } $name=mysql_result($query,0,"name"); $email=mysql_result($query,0,"email"); mysql_free_result($query); } ?>
另外一個(gè)參考的例程:
<?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 db include 'db_connect.php'; //SQL query to find if this entered username/password is in the db $sql = "SELECT * FROM healthed_workshop_admin WHERE user = '$PHP_AUTH_USER' AND pass = '$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!'; } ?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php metaphone()函數(shù)及php localeconv() 函數(shù)實(shí)例解析
- php htmlentities()函數(shù)的定義和用法
- PHP數(shù)組函數(shù)知識(shí)匯總
- php similar_text()函數(shù)的定義和用法
- php格式化json函數(shù)示例代碼
- php ucwords() 函數(shù)將字符串中每個(gè)單詞的首字符轉(zhuǎn)換為大寫(xiě)(實(shí)現(xiàn)代碼)
- Thinkphp單字母函數(shù)使用指南
- php構(gòu)造函數(shù)與析構(gòu)函數(shù)
- PHP preg match正則表達(dá)式函數(shù)的操作實(shí)例
- PHP錯(cuò)誤處理函數(shù)
- php metaphone()函數(shù)的定義和用法
相關(guān)文章
php基于表單密碼驗(yàn)證與HTTP驗(yàn)證用法實(shí)例
這篇文章主要介紹了php基于表單密碼驗(yàn)證與HTTP驗(yàn)證用法,以實(shí)例形式較為詳細(xì)的分析了表單密碼驗(yàn)證與HTTP驗(yàn)證的原理與相關(guān)注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01如何使用Laravel Eloquent來(lái)開(kāi)發(fā)無(wú)限極分類(lèi)
在網(wǎng)上商城上,我們經(jīng)??梢钥吹蕉嗉?jí)分類(lèi)、子分類(lèi)、甚至無(wú)限極分類(lèi)。本文將向你展示如何優(yōu)雅的通過(guò) Laravel Eloquent 將其實(shí)現(xiàn)。2021-05-05PHP中VC6、VC9、TS、NTS版本的區(qū)別與用法詳解
ISAPI執(zhí)行方式是以DLL動(dòng)態(tài)庫(kù)的形式使用,可以在被用戶(hù)請(qǐng)求后執(zhí)行,在處理完一個(gè)用戶(hù)請(qǐng)求后不會(huì)馬上消失,所以需要進(jìn)行線程安全檢查2013-10-10php使用$_POST或$_SESSION[]向js函數(shù)傳參
php向js函數(shù)傳參的方法有多種,可以使用$_POST也可使用$_SESSION[' '],也可用echo語(yǔ)句進(jìn)行輸出,需要的朋友可以參考下2014-09-09php獲取ip及網(wǎng)址的簡(jiǎn)單方法(必看)
下面小編就為大家?guī)?lái)一篇php獲取ip及網(wǎng)址的簡(jiǎn)單方法(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04