Page 1 of 1

http

Posted: Sun Jan 19, 2003 8:56 am
by jamal
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>'; 
} 

?>

Posted: Sun Jan 19, 2003 9:11 am
by Bill H
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.

Posted: Sun Jan 19, 2003 9:31 am
by jamal
yeah thanks.
My problem is how to each process to check where the failure actually is.
Can you help me please???

Posted: Sun Jan 19, 2003 10:36 am
by jamal
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>'; 
} 

?>

Posted: Sun Jan 19, 2003 12:05 pm
by Bill H

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";
     }

?>

Posted: Sun Jan 19, 2003 5:09 pm
by volka
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

Posted: Sun Jan 19, 2003 5:35 pm
by jamal
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>'; 
} 

?>

Posted: Sun Jan 19, 2003 5:45 pm
by volka

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