Jump to content

[SOLVED] fread not working right


anolan13

Recommended Posts

Hi everyone,

 

I'm trying to make a fairly simple login system based off of a couple bits of code and its not working right, hope you can help...

 

So the page before I make a session after the person registers successfully with logname which is just the name he logged in with, I also write a text file with the contents being that same logname. Here's my code on the protected page.

 

$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
session_start();
if (@$_SESSION['logname'] != $theData)
{
header("Location: http://mywebsite.com/login.html");
exit();
}

 

If I echo command $theData I see what it's contents are. If I type in $theData as the persons logname  directly it works correctly and allows the rest of the page to run. But for some reason it won't accept this block of code, it keeps kicking me back to mywebsite.com/login.html.

 

I'm very confused, please help!

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/69275-solved-fread-not-working-right/
Share on other sites

That's a "newline" character.  It shows up in echo as an actual new line, so that's why you don't see it.  The easiest way to deal with it is to use trim() on your data from fread().  trim() will remove spaces, newlines and some other invisible stuff from the start and end.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.