PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
jamal
Forum Commoner
Posts: 57 Joined: Sat Oct 26, 2002 7:53 pm
Location: London
Post
by jamal » Sun Jan 19, 2003 8:56 am
Hi Guys,
Please can anyone tell me why this
code is not working for me??
I have got some usernames and passwords in file.txt
but the fopen is not reading it.
Can anyone help me out here??
Thanks
Code: Select all
<?php
$auth = false; // Assume user is not authenticated
if (isset( $PHP_AUTH_USER ) && isset($PHP_AUTH_PW)) {
$filename = 'c:\\inetpub\\wwwroot\\http_cookie\\file.txt';
$fp = fopen( $filename, 'r' );
$file_contents = fread( $fp, filesize( $filename ) );
fclose( $fp );
$lines = explode ( "\n", $file_contents );
foreach ( $lines as $line ) {
list( $username, $password ) = explode( ':', $line );
if ( ( $username == "$PHP_AUTH_USER" ) &&
( $password == "$PHP_AUTH_PW" ) ) {
// A match is found, meaning the user is authenticated.
// Stop the search.
$auth = true;
break;
}
}
}
if ( ! $auth ) {
header( 'WWW-Authenticate: Basic realm="Private"' );
header( 'HTTP/1.0 401 Unauthorized' );
echo 'Authorization Required.';
exit;
} else {
echo '<P>You are authorized!</P>';
}
?>
Bill H
DevNet Resident
Posts: 1136 Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:
Post
by Bill H » Sun Jan 19, 2003 9:11 am
As a general programming rule it is a good idea to test each function for success.
For instance, you open the file, but you do not test to see if the open was successful before reading.
Then you do not test to see if the read was successful before outputting.
So the failure could be that the file was not opened, or the read failed, or...
If you test each step of the process you will know where the process is failing.
jamal
Forum Commoner
Posts: 57 Joined: Sat Oct 26, 2002 7:53 pm
Location: London
Post
by jamal » Sun Jan 19, 2003 9:31 am
yeah thanks.
My problem is how to each process to check where the failure actually is.
Can you help me please???
jamal
Forum Commoner
Posts: 57 Joined: Sat Oct 26, 2002 7:53 pm
Location: London
Post
by jamal » Sun Jan 19, 2003 10:36 am
This is what I have done so far .
If there still a help you can do for me???
Code: Select all
<?php
$auth = false;
if (isset( $PHP_AUTH_USER ) && isset($PHP_AUTH_PW)) {
$filename = 'c:\\php_data\\file.txt';
$fp = @fopen( $filename, 'r' ) || die("cannot open $filename");
$file_contents = fread( $fp, filesize( $filename ) ) || die("cannot read $fp");
fclose( $fp );
$lines = explode ( "\n", $file_contents );
$PHP_AUTH_PW.
foreach ( $lines as $line ) {
list( $username, $password ) = explode( ':', $line );
if ( ( $username == "$PHP_AUTH_USER" ) &&
( $password == "$PHP_AUTH_PW" ) ) {
$auth = true;
break;
}
}
}
if ( ! $auth ) {
header( 'WWW-Authenticate: Basic realm="Private"' );
header( 'HTTP/1.0 401 Unauthorized' );
echo 'Authorization Required.';
exit;
} else {
echo '<P>You are authorized!</P>';
}
?>
Bill H
DevNet Resident
Posts: 1136 Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:
Post
by Bill H » Sun Jan 19, 2003 12:05 pm
Code: Select all
<?php
$f = @fopen($name, "r"); // open file for reading, suppress error msg
if ($f != FALSE)
{ $a = @fread($f, filesize($name)); // read the entire file
fclose($f);
}
else
{
echo "Could not open file";
}
?>
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sun Jan 19, 2003 5:09 pm
http://www.php.net/manual/en/debugger.php
takes some time to install and get used to a debugger but the more complex a project gets the more valuable it will become
jamal
Forum Commoner
Posts: 57 Joined: Sat Oct 26, 2002 7:53 pm
Location: London
Post
by jamal » Sun Jan 19, 2003 5:35 pm
this is the modifictation of my code and yet I am not lucky enough to have it working:
Code: Select all
<?php
$auth = false;
if (isset( $PHP_AUTH_USER ) && isset($PHP_AUTH_PW)) {
$filename = 'c:\\php_data\\file.txt';
$f = @fopen($filename, "r");
if ($f != FALSE)
{ $a = @fread($f, filesize($filename));
fclose($f);
}
else
{
echo "Could not open file";
}
$lines = explode ( "\n", $file_contents );
foreach ( $lines as $line ) {
list( $username, $password ) = explode( ':', $line );
if ( ( $username == "$PHP_AUTH_USER" ) &&
( $password == "$PHP_AUTH_PW" ) ) {
$auth = true;
break;
}
}
}
if ( ! $auth ) {
header( 'WWW-Authenticate: Basic realm="Private"' );
header( 'HTTP/1.0 401 Unauthorized' );
echo 'Authorization Required.';
exit;
} else {
echo '<P>You are authorized!</P>';
}
?>
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sun Jan 19, 2003 5:45 pm
Code: Select all
<?php
$auth = false;
if (isset($PHP_AUTH_USER) AND isset($PHP_AUTH_PW)) // might be $_SERVER['PHP_AUTH_USER']/['PHP_AUTH_PW']
{
$lines = file('c:\\php_data\\file.txt') or die('could not open file');
$lines = array_map('trim', $lines);
$auth = in_array($PHP_AUTH_USER.':'.$PHP_AUTH_PW, $lines);
}
if ( ! $auth )
{
header( 'WWW-Authenticate: Basic realm="Private"' );
header( 'HTTP/1.0 401 Unauthorized' );
if(isset($PHP_AUTH_USER) AND isset($PHP_AUTH_PW))
echo 'Authorization Failed.';
else
echo 'Authorization Required.';
}
else
echo '<P>You are authorized!</P>';
?>but only if
array_map
(PHP 4 >= 4.0.6)
applies to your php-version