PDA

Click to See Complete Forum and Search --> : If Statement (i think)


Pino
Aug 8th, 2004, 04:28 AM
ok i'm trying to at the moment compare a password with a textfile, here my code

<?php
$user = $_POST['username'];
$pass = $_POST['password'];

$filecontents = file('person.data');

if ($pass == $filecontents[1])
{
echo "Welcome";
}
else
{
echo "Go away";
}


?>

but for some reason it allways says go away evan when i type the correct password in... any problems with my code? (i've only just got learning]

Pino
Aug 8th, 2004, 04:33 AM
it must be a problem with my if statement because when i echo the pass and the fcode from the file it equals the same

visualAd
Aug 8th, 2004, 06:01 AM
From the PHP manual (http://www.php.net/file):

Identical to readfile(), except that file() returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure, file() returns FALSE.

If you created the file in UNIX then the newline character will be a single "\n" if you created the file in Windows it will be CRLF ("\r\n").

Pino
Aug 8th, 2004, 11:44 AM
i dont understand, could you please show me how to correct my code?

visualAd
Aug 8th, 2004, 12:14 PM
The reason your strign is not match is becuase it has a newline character at the end of the string.

I.e.

"password" <> "password\n"

Where "\n" represents a new line character. In Windows files however, a new line is represented by two characters; a carridge return and linefeed ("\r\n" in PHP or vbCrlF in Visual Basic or ASCII codes 10 and 13).

The file function in PHP reads each line into elements of an array including the newline characters. So you should consider these characters when checking if the strings are equal. The trim() (http://uk.php.net/trim) function in PHP would do the trick as it takes any whitespace (tabs, newlines and spaces) from the beginning and the end of the string.

if ($pass == trim ($filecontents[1]))

Pino
Aug 8th, 2004, 12:36 PM
arr ok got you, it makes sence now, 2 more questions to pick you mind at...

1 - what if the user had a two word password (just lets say i allowed it)

2 - is there another way to read from the file that doesnt keep the newlines in?

thanks again

visualAd
Aug 8th, 2004, 12:59 PM
With the code you are using you shouldn't have a problem with two word passwords.

All the functions in PHP that read file contents return newline characters too. You could use fscanf function but this is slower than using Trim() which does the job anyway.

The Hobo
Aug 8th, 2004, 01:24 PM
It might create a problem if users add a space at the end or beginning of the password. trim() will remove it and the passwords won't match. Maybe a preg_replace() to get rid of newlines/feeds at the end.

visualAd
Aug 8th, 2004, 01:56 PM
This should solve your problem:

<?php
/* like the fgets() function but strips off the end of line characters
* DOS, UNIX and MAC compatible
*/
function fgetline ($hwnd) {
$line = fgets ($hwnd);

if (preg_match ("/(.*)[\r?\n|\r]/", $matches))
return $matches[1];
else
return $line;
}
?>

Regular expressions save the day again :bigyello:

The Hobo
Aug 8th, 2004, 03:54 PM
Originally posted by visualAd

Regular expressions save the day again :bigyello:

My hero. :afrog: