php使用Header函數,PHP_AUTH_PW和PHP_AUTH_USER做使用者驗證_php技巧

來源:互聯網
上載者: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相關內容感興趣的讀者可查看本站專題:《PHP網路編程技巧總結》、《PHP基本文法入門教程》、《php操作office文檔技巧總結(包括word,excel,access,ppt)》、《php日期與時間用法總結》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.