|
PHP源码:
<?php
function auth()
{
header("WWW-Authenticate: Basic realm=www.fish.com");
header("HTTP/1.1 401 Unauthorized");
echo "<h2>您需要进行身份认证!</h2>";
echo "<br>请重新选择刷新,重新加载!";
exit;
}
//显示欢迎信息
function showWelcome($msg="祝贺您,您已经通过了身份认证!")
{
echo "<table border='0'>";
echo "<tr><td align = 'center'><h4>";
echo "<font color=#ff0000>$msg</font>";
echo "</h4></td></tr>";
echo "<table>";
}
//检查变量$PHP_AUTH_USER
if(!isset($PHP_AUTH_USER))
{
//身份认证信息发送
auth();
}
else {
//用户信息设定,则连接数据库进行检查
mysql_connect("localhost","root","******")
or die("Unable to connect to database!");
mysql_select_db("web_db")
or die("Unable to select database!");
//设置查询语句,语句中使用PASSWORD函数
$query="select usr_id
from http_auth
where uname='$PHP_AUTH_USER'
and userpass='PASSWORD('$PHP_AUTH_pw')";
//数据库查询
$result=mysql_query($query);
$numRows=mysql_num_rows($result);
//判断结果
if($numRows==0){
// 用户不存在,身份不合法
//重新发送认证信息
auth();
}
else {
//用户身份合法,显示欢迎信息
showWelcome();
}
}
?>
-------------------------------------------------------------------------------------------
浏览器返回的错误信息:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/fish/PHP操作MYSQL/php身份认证.php on line 43
Warning: Cannot add header information - headers already sent by (output started at /var/www/html/fish/PHP操作MYSQL/php身份认证.php:43) in /var/www/html/fish/PHP操作MYSQL/php身份认证.php on line 4
Warning: Cannot add header information - headers already sent by (output started at /var/www/html/fish/PHP操作MYSQL/php身份认证.php:43) in /var/www/html/fish/PHP操作MYSQL/php身份认证.php on line 5
您需要进行身份认证!
请重新选择刷新,重新加载!
请问各路高手是什么问题!???
谢谢~~~~~ |
|