Problem using file_get_contents

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
paulmac1966
Forum Newbie
Posts: 16
Joined: Mon Feb 13, 2006 9:37 am

Problem using file_get_contents

Post 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
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

what var_dump($password);

says ??
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I usually do

Code: Select all

if ($file = file_get_contents('password.txt')) {
   echo $file;
}
else {
   echo 'failed';
}
paulmac1966
Forum Newbie
Posts: 16
Joined: Mon Feb 13, 2006 9:37 am

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

Post 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);
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post 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';
}
paulmac1966
Forum Newbie
Posts: 16
Joined: Mon Feb 13, 2006 9:37 am

Post 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.
paulmac1966
Forum Newbie
Posts: 16
Joined: Mon Feb 13, 2006 9:37 am

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

Post by feyd »

It doesn't add any characters. Whatever/however you wrote the file added those characters. file_get_contents() is binary safe.
Post Reply