在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\']
获取到用户提交上来的用户名和密码之后,要怎样处理逻辑就跟我们一般的程序处理没有什么区别了。下面提供两个例程供参考:
<?
if(!isset($PHP_AUTH_USER)) {
Header(\"WWW-authenticate: basic realm=\"XXX\"\");
Header(\"HTTP/1.0 401 Unauthorized\");
$ =\"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>
<?
exit;
} else {
mysql_pconnect(\"localhost\",\"nobody\",\"\") or die(\"Unable to connect to SQL server\");
mysql_select_db(\"xxx\") or die(\"Unable to select data \");
$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\");
$ =\"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>
<?
exit;
}
$name=mysql_result($query,0,\"name\");
$email=mysql_result($query,0,\"email\");
mysql_free_result($query);
}
?> 继续阅读与本文标签相同的文章
-
手机导航是怎样判断路况的?不仅是通过卫星,这几个原因使导航更智能
2026-05-14栏目: 教程
-
手机信号突然从“4G”变成“E”,是什么意思?客服给出答案
2026-05-14栏目: 教程
-
互联网架起“乌镇式生活”
2026-05-14栏目: 教程
-
微信才是内存“杀手”,别再乱清理,关闭这个功能,手机立马流畅
2026-05-14栏目: 教程
-
手机信号变成E是什么意思?看完专业人士给出的解释后,涨知识了
2026-05-14栏目: 教程
