register_globals=off
Date: 07/15/06
(PHP Community) Keywords: php
I'm aware that register_globals=off at the ISP problem will not let me use $PHP_AUTH_USER which is dangerous anyway. I think I got around it by the first two lines in my code below, but I just keep failing authentication. This use to work on my old server. Any ideas?
$PHP_AUTH_USER=$HTTP_SERVER_VARS[PHP_AUTH_USER];
$PHP_AUTH_PW=$HTTP_SERVER_VARS[PHP_AUTH_PW];
$auth = false; // Assume user is not authenticated
if (isset( $PHP_AUTH_USER ) && isset($PHP_AUTH_PW)) {
// Connect to MySQL
mysql_connect( "localhost", "user", "pass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("mydb" );
// Formulate the query
$sql = "SELECT * FROM users WHERE
username = '$PHP_AUTH_USER' AND
password = '$PHP_AUTH_PW'";
// Execute the query and put results in $result
$result = mysql_query( $sql )
or die ( 'Unable to execute query.' );
// Get number of rows in $result.
$num = mysql_numrows( $result );
if ( $num != 0 ) {
// A matching row was found - the user is authenticated.
$auth = true;
}
}
if ( ! $auth ) {
header( 'WWW-Authenticate: Basic realm="Admin"' );
header( 'HTTP/1.0 401 Unauthorized' );
echo 'Authorization Required.';
exit;
}
?>
Source: http://community.livejournal.com/php/470277.html