Forum Moderators: coopster
<?php
$dbh=mysql_connect ("localhost", "db_username", "db_password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("db_name");
$authorized = FALSE;
if ((isset($_SERVER['PHP_AUTH_USER']) AND isset($_SERVER['PHP_AUTH_PW'])) ) {
$query ="SELECT username FROM users WHERE username='{$_SERVER['PHP_AUTH_USER']}' and
password='{$_SERVER['PHP_AUTH_PW']}'";
$result = mysql_query ($query);
$row = @mysql_fetch_array ($result);
if ($row) {
$authorized = TRUE;
}
}
if (!$authorized){
header('WWW-Authenticate: Basic realm="localhost"');
header('HTTP/1.0 401 Unauthorized');
}
?>
<html>
etc
</html>
instead of
if ($row) {
Also, you have to be very careful when doing logins. It migh be better to do it like this:
$query ="SELECT username, password FROM users WHERE username='{$_SERVER['PHP_AUTH_USER']}' and
password='{$_SERVER['PHP_AUTH_PW']}'";
$result = mysql_query ($query);
$row = @mysql_fetch_array ($result);
IF (($row['username']==$_SERVER['PHP_AUTH_USER']) && ($row['password']==$_SERVER['PHP_AUTH_PW'])) {
.
.
Why? Well, if the user can get this string:
' or password like='%
to be accepted as the password and username1 as the user, then you would be in trouble. The query would look like this:
SELECT username FROM users WHERE username='username1' and
password='' or password like '%'
The way you have the code now, this would always let him login as username1.