我在让 HTTP 授权工作时遇到了一些困难。由于某种原因,我一直得到的只是弹出框询问用户名和密码。这是我正在使用的代码。有人可以告诉我哪里出错了吗?
if ( !isset( $_SERVER['PHP_AUTH_USER'] ) || !isset( $_SERVER['PHP_AUTH_PW'] ) )
{
authorizeUser();
//if no result, there is no user
}
else
{
$user = htmlentities( trim( $_SERVER['PHP_AUTH_USER'] ) );
$pass = htmlentities( trim( $_SERVER['PHP_AUTH_PW'] ) );
$pass = md5( $_SERVER['PHP_AUTH_PW'] );
//connect to MySQL data here//
$sql = "SELECT user_name,pwd FROM tableName WHERE user_name='$user' AND pwd='$pass'";
$result = mysql_query( $sql );
if( mysql_num_rows( $result ) == 0 )
{
authorizeUser();
}
else
{
header( "Location: somepage" );
}
}
function authorizeUser( )
{
header( 'WWW-Authenticate: Basic realm="Restricted Area"' );
header( 'HTTP/1.0 401 Unauthorized' );
echo "text if cancel button used";
}//end authorizedUser