|
-
Aug 8th, 2004, 04:28 AM
#1
Thread Starter
PowerPoster
If Statement (i think)
ok i'm trying to at the moment compare a password with a textfile, here my code
PHP 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]
-
Aug 8th, 2004, 04:33 AM
#2
Thread Starter
PowerPoster
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
-
Aug 8th, 2004, 06:01 AM
#3
From the PHP manual:
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").
-
Aug 8th, 2004, 11:44 AM
#4
Thread Starter
PowerPoster
i dont understand, could you please show me how to correct my code?
-
Aug 8th, 2004, 12:14 PM
#5
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() 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.
PHP Code:
if ($pass == trim ($filecontents[1]))
-
Aug 8th, 2004, 12:36 PM
#6
Thread Starter
PowerPoster
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
-
Aug 8th, 2004, 12:59 PM
#7
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.
-
Aug 8th, 2004, 01:24 PM
#8
Stuck in the 80s
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.
-
Aug 8th, 2004, 01:56 PM
#9
This should solve your problem:
PHP Code:
<?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
-
Aug 8th, 2004, 03:54 PM
#10
Stuck in the 80s
Originally posted by visualAd
Regular expressions save the day again
My hero.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|