/    Sign up×
Community /Pin to ProfileBookmark

PHP – Reading Files

For some reason, basic logic doesn’t work with this code…

I am making a very simple(and somewhat poor) chat system. Anyways, I am trying to implement a way so that the same user can not log on twice, which will prevent other problems, including logging off issues.

This is the only part that doesn’t work. It opens the user list file and reads it line by line. The file has one user per line. But for some reason, even if the line that is read equals(exactly) the users name, then it will still not exit and pretends that the two do not match and so the two users can log on with the same name.

[code=php]
$currentUserList = fopen($user_file, “r”) or exit(“Unable to open file!”);

while(!feof($currentUserList))
{
$tmpUser = fgets($currentUserList);
if($tmpUser == $getName)
{
exit(‘name taken’);
}
}
[/code]

I have echoed the code back and seen that the two values are indeed equal, but yet the code does not exit, and so the script continues on to log in the user.

Does anyone have any ideas? I’ve tried this several ways, and none of them have worked yet.

I really would like to keep any methods simple.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@jasonahouleSep 22.2007 — Is there any whitespace? Try to use if(trim($tmpUser) == trim($getName))
Copy linkTweet thisAlerts:
@Sup3rkirbyauthorSep 22.2007 — Why that was exactly the problem. There was a silly little blank space after the $tmpUser.

Probably has something to do with it reading from the file. Anyways, looks like that did it. Now the log in system is fixed so it will prevent the same name from logging in.
Copy linkTweet thisAlerts:
@Sup3rkirbyauthorSep 22.2007 — Where's the edit button? I swear there was one before.... Is there some condition in which it may go away?

Hmmm..... Anyways, while your insight helped, there still remains one problem. Altough it is a different problem, it still pertains to the overall situation, as well as the topic subject.

I believe I mentioned that this was for a simple chatroom-like application. Well, my current method is simple. You have a server file that holds all the usernames on that server. When a user joins, a text file is created to hold all their chat data. Whenever a user sends a message or takes an action, then a loop is run to send the data to all the users(in their text file).

Well, this all works but it seems it is over working. I always get an extra file(_defualt.txt)('default' is simply the name of the server, so this is replaced with any server name). I think that with the 'eof()' function, it loops through to add the new data to each user, but when it reaches the end, there is a new line that is blank(my code adds a newline each time a new user is added, but it results in one extra, leaving a blank line at the end).


So yea.... any ideas? here is the part of the code that adds users.
[code=php]
$userData = file_get_contents($user_file);

$fp = fopen($user_file, 'w') or exit("Unable to write new user!");
if($userData != "")
{
fwrite($fp, $userData . "n" . $getName);
} else {
fwrite($fp, $getName);
}
fclose($fp);
[/code]


I thought with the if statement it would only add a new line once the file had some data in it, but it always adds the newline....
Copy linkTweet thisAlerts:
@Wisest_GuySep 22.2007 — Is the file emptied properly when the last user logs out?
Copy linkTweet thisAlerts:
@Sup3rkirbyauthorSep 23.2007 — I could only think so, but it may be possible that it is not. And in that case, what would be the best way to empty the file with PHP? $user_file is the variable for the filename. I'm still pretty new to PHP, and I kind of jumped into the whole file operations thing.
Copy linkTweet thisAlerts:
@Wisest_GuySep 27.2007 — Aw screw it, use line breaks.
[code=php]
function LogIn($user)
$fp = fopen($user_file,"a") or exit("Aw screw it, get IRC.");
fwrite($fp,$user."n");
fclose($fp);
}

function NameFree($user)
return (strpos(file_get_contents($user_file),$user."n") === false);
}

function LogOut($user)
$list = file_get_contents($user_file);
$fp = fopen($user_file,"w") or exit("Aw screw it, get IRC.");
fwrite($fp,str_replace($user."n","",$list));
fclose($fp);
}
[/code]
×

Success!

Help @Sup3rkirby spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...