php使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶驗(yàn)證
來(lái)源:易賢網(wǎng) 閱讀:797 次 日期:2016-08-25 14:42:01
溫馨提示:易賢網(wǎng)小編為您整理了“php使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶驗(yàn)證”,方便廣大網(wǎng)友查閱!

本文實(shí)例講述了php使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶驗(yàn)證的方法。分享給大家供大家參考,具體如下:

在php中,可以使用Header函數(shù)做一些有趣的事情,用戶驗(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è)登錄框,要求輸入用戶名和密碼。習(xí)慣了在頁(yè)面登錄的我們,是否覺得這樣的登錄很原始,又很新奇呢?

為了獲取從這個(gè)對(duì)話框中傳來(lái)的用戶名和密碼,需要用到php提供的兩個(gè)特殊變量$PHP_AUTH_USER和$PHP_AUTH_PW,要這樣使用這兩個(gè)特殊變量好像需要在php.ini中設(shè)置相關(guān)的選項(xiàng),不然就只能像下面這樣引用:

$_SERVER['PHP_AUTH_USER']

$_SERVER['PHP_AUTH_PW']

獲取到用戶提交上來(lá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!';

}

?>

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)