Page 1 of 1

Problem using file_get_contents

Posted: Fri Feb 17, 2006 10:37 am
by paulmac1966
I have written a log in page for an FTP server with a username and password, which works fine
when i use a predefined variable within the php code, but i dont want to do it that way.

I have a text file hidden on my web server and i want the php code to test to see if the password the user
entered is the same as the one stored in the text file.

It does not match when i enter the correct password, and when i was trying to debug it i found
that if i do a strlen on the text file it is adding 1 to the answer, ie i was expecting 7 and i get 8.

Code: Select all

$password=file_get_contents("password.txt");
print strlen($password);
i have tried using trim & trim but it does not make any difference

i know i could use substr and extract the 1st 7 char's but this seems a bit OTT.
I am assuming it is counting a (/n) newline char.

Can anyone tell me a simple way of getting the correct answer from strlen.

Thanks

Posted: Fri Feb 17, 2006 10:53 am
by duk
what var_dump($password);

says ??

Posted: Fri Feb 17, 2006 11:04 am
by John Cartwright
I usually do

Code: Select all

if ($file = file_get_contents('password.txt')) {
   echo $file;
}
else {
   echo 'failed';
}

Posted: Mon Feb 20, 2006 2:53 am
by paulmac1966
jcart

I have similar code which you posted, that is not the problem, the problem is that it allways says
the password is wrong even when i enter the correct password.

Which is why i started to examine what it gets when it reads the text file which is when
i discovered that the string length (strlen) is 8 when i would expect it to be 7.

I think it is counting an extra char (possibly newline).

i am using

Code: Select all

$MYusername=($_POST['username']);
$MYpasswd=($_POST['passwd']);

$username=file_get_contents("username.txt");
$passwd=file_get_contents("passwd.txt");

if ($MYusername == $username && MYpasswd == $passwd)
       { print "Username and Password ok";}
else
       {print "Invalid Login"}

Posted: Mon Feb 20, 2006 3:08 am
by feyd
how did you create these files?

If you created them in a text editor, it may add extra characters. Checking the raw binary version of the contents may tell you something.

Code: Select all

$username = file_get_contents('username.txt');
$rawusername = implode('',unpack('H*',$username));
var_export($rawusername);

Posted: Mon Feb 20, 2006 3:59 am
by quocbao
Jcart wrote:I usually do

Code: Select all

if ($file = file_get_contents('password.txt')) {
   echo $file;
}
else {
   echo 'failed';
}
I think it should be

Code: Select all

if (($file = file_get_contents('password.txt')) !== false) {
   echo $file;
}
else {
   echo 'failed';
}

Posted: Mon Feb 20, 2006 4:08 am
by paulmac1966
i tried using the raw binary format

i got a string of chars that mean nothing to me

can you explain how i interperet the output.

I used vi to enter the username and password into a txt file, on the linux server.

Posted: Mon Feb 20, 2006 4:38 am
by paulmac1966
i found a work a round
not really what i wanted.

i used the fopen and fread commands
but specified the fread command read the 1st 7 chars of the text file, not the
8 chars it seems to think are in it.

All working ok now.

Thanks for your help.

But if anyone can explain why the file_get_contents seems to be
adding an extra char i would be interested to know why.
And how to prevent it, and before anyone suggests trim or rtrim
i have tried them both.

Posted: Mon Feb 20, 2006 10:00 am
by feyd
It doesn't add any characters. Whatever/however you wrote the file added those characters. file_get_contents() is binary safe.