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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.