Page 1 of 1

unexpected $ in flat file code :(

Posted: Wed May 19, 2004 1:47 pm
by friedcpu
Hi
Was wondering if you could tell me what is causing this error, and maybe how to fix it.

Error: Parse error: parse error, unexpected $ in validate.php on line 25

Line 25 contrains..

?>

thats it.

Here is all of the code, as line 25 tells you nothing
<?
// Define post fields into simple variables
$username = $_POST['username'];
$password = $_POST['password'];
$filename = 'users.txt';
$fp = fopen( $filename, 'r' );
$file_contents = fread( $fp, filesize( $filename ) );
fclose( $fp );

// Place the individual lines from the file contents into an array.

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


foreach ( $lines as $line ) {

list( $username, $password ) = explode( ':', $line );

if ( ( $username == "$username" ) &&
( $password == "$password" ) ){
echo "Success!";
}
?>
Thanks in advance to anybody who helps :)

Posted: Wed May 19, 2004 1:51 pm
by feyd
you're missing a } to close the foreach().. and you have a logic error in the last few lines.. checking $username == "$username" and $password == "$password" will pretty much always be true.

Posted: Wed May 19, 2004 1:51 pm
by launchcode
You don't close your foreach statement.

Also - you don't need to mess around with fopen and explode like that - why not just do this:

Code: Select all

$file_contents = file('users.txt);
it will do all of the fopen and explode stuff for you.

Posted: Wed May 19, 2004 1:56 pm
by friedcpu
thank you for you replies.

I will try the $file contents :)

as for the logic error, I know, i will add a else
after i got the error sorted.

I will go try what you suggest now. :)

EDIT

Got it to work
I misunderstood your comment about logic, I see it always returns success :? will have to get that sorted.

thanks for helping