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


Posted in PHP onMay 04, 2016

本文实例讲述了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程序设计有所帮助。

PHP 相关文章推荐
强烈推荐:php.ini中文版(1)
Oct 09 PHP
在php MYSQL中插入当前时间
Apr 06 PHP
php access 数据连接与读取保存编辑数据的实现代码
May 12 PHP
PHP开发中的错误收集,不定期更新。
Feb 03 PHP
php正则表达匹配中文问题分析小结
Mar 25 PHP
PHP开发工具ZendStudio下Xdebug工具使用说明详解
Nov 11 PHP
php使用$_POST或$_SESSION[]向js函数传参
Sep 16 PHP
为你总结一些php系统类函数
Oct 21 PHP
php数据序列化测试实例详解
Aug 12 PHP
Laravel构建即时应用的一种实现方法详解
Aug 31 PHP
php模拟实现斗地主发牌
Apr 22 PHP
php优化查询foreach代码实例讲解
Mar 24 PHP
PHP实现获取并生成数据库字典的方法
May 04 #PHP
PHP创建/删除/复制文件夹、文件
May 03 #PHP
Yii2使用swiftmailer发送邮件的方法
May 03 #PHP
php读取torrent种子文件内容的方法(测试可用)
May 03 #PHP
Yii2 输出xml格式数据的方法
May 03 #PHP
php面向对象值单例模式
May 03 #PHP
php使用ffmpeg获取视频信息并截图的实现方法
May 03 #PHP
You might like
PHP 高级课程笔记 面向对象
2009/06/21 PHP
php不使用插件导出excel的简单方法
2014/03/04 PHP
PHP获取一个字符串中间一部分字符的方法
2014/08/19 PHP
php实现的数字验证码及数字运算验证码
2015/07/30 PHP
Symfony实现行为和模板中取得request参数的方法
2016/03/17 PHP
浅谈php数组array_change_key_case() 函数和array_chunk()函数
2016/10/22 PHP
PHP扩展Swoole实现实时异步任务队列示例
2019/04/13 PHP
javascript高亮效果的二种实现方法
2008/09/14 Javascript
用js通过url传参把数据从一个页面传到另一个页面
2014/09/01 Javascript
JS组件中bootstrap multiselect两大组件较量
2016/01/26 Javascript
jQuery Ajax 上传文件处理方式介绍(推荐)
2016/06/30 Javascript
浅谈js中调用函数时加不加括号的问题
2016/07/28 Javascript
AngularJS入门教程之ng-class 指令用法
2016/08/01 Javascript
vue bus全局事件中心简单Demo详解
2018/02/26 Javascript
基于jquery实现九宫格拼图小游戏
2018/11/30 jQuery
Swiper.js实现移动端元素左右滑动
2019/09/08 Javascript
原生js实现日期选择插件
2020/05/21 Javascript
微信小程序学习总结(五)常见问题实例小结
2020/06/04 Javascript
Chrome插件开发系列一:弹窗终结者开发实战
2020/10/02 Javascript
Python爬虫代理IP池实现方法
2017/01/05 Python
django 常用orm操作详解
2017/09/13 Python
Python数据可视化编程通过Matplotlib创建散点图代码示例
2017/12/09 Python
Linux下python3.6.1环境配置教程
2018/09/26 Python
python实现将中文日期转换为数字日期
2020/07/14 Python
浅谈Selenium+Webdriver 常用的元素定位方式
2021/01/13 Python
用纯css3和html制作泡沫对话框实现代码
2013/03/21 HTML / CSS
canvas像素画板的实现代码
2018/11/21 HTML / CSS
将SVG图引入到HTML页面的实现
2019/09/20 HTML / CSS
Overload和Override的区别
2012/09/02 面试题
专营店会计助理岗位职责
2013/11/29 职场文书
《九寨沟》教学反思
2014/04/08 职场文书
人事行政经理岗位职责
2014/06/18 职场文书
公司周年庆活动方案
2014/08/25 职场文书
2016大学生社会实践单位评语
2015/12/01 职场文书
90条交通安全宣传标语
2019/10/12 职场文书
Windows Server 2012 R2服务器安装与配置的完整步骤
2022/07/15 Servers