users logged in or not in php?

2008-12-11 3:23 am
Hey, I couldn't figure a way to work in seeing if users were logged in or not on my site, so I just used a simple UPDATE to the sql under each username when they went through the log in process, as shown below:

$update = mysql_query("UPDATE `members` SET `online` = '[ONLINE]' WHERE `username` = '$logged[username]'");

so it updates the online field with the word "[ONLINE]" and under each username, i just call $online, to show the text.

well to show that they've logged out, i put in the logout.php page, just the opposite,

$update = mysql_query("UPDATE `members` SET `online` = '' WHERE `username` = '$logged[username]'");

which just changes the text "[ONLINE]" to ""

though what i didn't think of is that most users to log out, they just close the page.. so without going through the logout process, i'm left with about every user shown as online.. forever.

So i tried putting on the index.php

basically, if user is logged, then change to [ONLINE] else, change to ""

though when they exit the site, that doesn't run the ELSE command really.. so is there a way I can show users as offline or pretty much just take away the [ONLINE] text if they simply close the website?

回答 (6)

2008-12-11 3:48 am
✔ 最佳答案
The problem with your methodology is that most people won't bother logging out. Other than that, it's fine.

Because the Web is stateless, you cannot know certainly whether anyone is logged on or not.

There are approximations you can make, however, that come close. Most people use session iteration, and it's discussed here:

http://www.devarticles.com/c/a/PHP/The-Quickest-Way-To-Count-Users-Online-With-PHP/1/

This kind of system can really hammer performance, however. Expect it to work slowly.
2016-10-25 9:57 pm
Your good judgment seems ok- assuming $accounttype is determined correct. you have not reported what's getting output although so i visit't understand the mission completely. i'd commence through outputting echo 'account form is ' . $accounttype ; after it really is determined. likely you have not got any fee assign to this variable.
2008-12-12 4:20 pm
Just change everything
參考: being smarter then Matthew
2008-12-11 3:35 am
A more practical way of doing it is use a datetime field to represent the timestamp of the last request from the user instead of just plain text of "online", "offline", or whatever. Then you'll have to decide the longest inactive time interval where user is considered online. When request is received from the user, update the datetime field. Compare the current timestamp with the datetime field when you do the online/offline check.

It's just my suggestion and what I usually do, maybe there are some better ways I don't know.
2008-12-11 3:31 am
This might not be the best way but the way i have always done it is to create a time stamp in the database when the user logs on. Everytime the user changes page update the timestamp to current time.

Then in the show users that are online portion select users that have a time stamp within the last 5minutes.

So if a user is inactive for 5minutes they are not shown.

You can change the inactive time to whatever you want :).

2008-12-11 3:30 am
Just use cookies or session in your application. You also need a script which will be executed via crontab. This script being executed by crontab checks the session or cookies of the users from time to time whether that session or cookies is already expired or not, then do the usual update to your database regarding the online statuses of your members..


Goodluck


收錄日期: 2021-05-01 01:01:11
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20081210192333AAjNKYg

檢視 Wayback Machine 備份