足球游戏_中国足彩网¥体育资讯$

php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证
来源:易贤网 阅读:764 次 日期:2016-08-25 14:42:01
温馨提示:易贤网小编为您整理了“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']

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

<?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);

}

?>

另外一个参考的例程:

<?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!';

}

?>

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

中国足彩网信息请查看网络编程
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标