unexpected $ in flat file code :(

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

Post Reply
friedcpu
Forum Newbie
Posts: 4
Joined: Wed May 19, 2004 1:47 pm
Location: uk
Contact:

unexpected $ in flat file code :(

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
friedcpu
Forum Newbie
Posts: 4
Joined: Wed May 19, 2004 1:47 pm
Location: uk
Contact:

Post 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
Post Reply