PDA

View Full Version : php user authentication help



The_Net_Muppet
Tue 4th Jul '00, 4:57am
can anyone tell me why it wont authenticate the username and password please?


auth.php:
=========




<?php

// File Name: auth03.php

// Check to see if $PHP_AUTH_USER already contains info

if (!isset($PHP_AUTH_USER)) {

// If empty, send header causing dialog box to appear

header('WWW-Authenticate: Basic realm="My Private Stuff"');
header('HTTP/1.0 401 Unauthorized');
exit;

} else if (isset($PHP_AUTH_USER)) {

// If non-empty, open file containing valid user info

$filename = "/path/to/file.txt";
$fp = fopen($filename, "r");
$file_contents = fread($fp, filesize($filename));
fclose($fp);

// Place each line in user info file into an array

$line = explode("\n", $file_contents);

// For as long as $i is <= the size of the $line array,
// explode each array element into a username and password pair


$i = 0;

while($i <= sizeof($line)) {
$data_pair = explode(":", $line[$i]);

if (($data_pair[0] == "$PHP_AUTH_USER") && ($data_pair[1] ==
"$PHP_AUTH_PW")) {
$auth = 1;
break;
} else {
$auth = 0;
}
$i++;
}

if ($auth == "1") {

echo "<P>You're authorized!</p>";
exit;

} else {

header('WWW-Authenticate: Basic realm="My Private Stuff"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authorization Required.';
exit;

}
}

?>



file.txt:
=========




guest:guest