目錄
php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证,basicauthheader
首頁 後端開發 php教程 php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证,basicauthheader_PHP教程

php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证,basicauthheader_PHP教程

Jul 12, 2016 am 08:53 AM
header php 用戶驗證

php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证,basicauthheader

本文实例讲述了php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证的方法。分享给大家供大家参考,具体如下:

在php中,可以使用Header函数做一些有趣的事情,用户验证就是其中一个很有意思的功能。具体用法:

1

2

Header("WWW-Authenticate: Basic realm="USER LOGIN"");

Header("HTTP/1.0 401 Unauthorized");

登入後複製

在页首设计这两个Header函数,页面在载入前会出现一个登录框,要求输入用户名和密码。习惯了在页面登录的我们,是否觉得这样的登录很原始,又很新奇呢?

为了获取从这个对话框中传来的用户名和密码,需要用到php提供的两个特殊变量$PHP_AUTH_USER和$PHP_AUTH_PW,要这样使用这两个特殊变量好像需要在php.ini中设置相关的选项,不然就只能像下面这样引用:

1

2

$_SERVER['PHP_AUTH_USER']

$_SERVER['PHP_AUTH_PW']

登入後複製

获取到用户提交上来的用户名和密码之后,要怎样处理逻辑就跟我们一般的程序处理没有什么区别了。下面提供两个例程供参考:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

<&#63;php

if(!isset($PHP_AUTH_USER)) {

Header("WWW-authenticate: basic realm="XXX"");

Header("HTTP/1.0 401 Unauthorized");

$title="Login Instructions";

&#63;>

<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>

<&#63;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";

&#63;>

<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>

<&#63;php

exit;

}

$name=mysql_result($query,0,"name");

$email=mysql_result($query,0,"email");

mysql_free_result($query);

}

&#63;>

登入後複製

另外一个参考的例程:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<&#63;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!';

}

&#63;>

登入後複製

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125236.htmlTechArticlephp使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证,basicauthheader 本文实例讲述了php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证的方法。分...
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

CakePHP 日期和時間 CakePHP 日期和時間 Sep 10, 2024 pm 05:27 PM

CakePHP 日期和時間

CakePHP 專案配置 CakePHP 專案配置 Sep 10, 2024 pm 05:25 PM

CakePHP 專案配置

CakePHP 檔案上傳 CakePHP 檔案上傳 Sep 10, 2024 pm 05:27 PM

CakePHP 檔案上傳

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

CakePHP 路由

討論 CakePHP 討論 CakePHP Sep 10, 2024 pm 05:28 PM

討論 CakePHP

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 快速指南

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 Dec 20, 2024 am 11:31 AM

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發

See all articles