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!
i have a web page that contains a login form,now when the user logged in i set some flag to 1 which use to detect online users,and when the user logg out of course set the flag to 0
but the problem is when the user close his browser without click log off the flag save its value (1)
and the user seem as if online permanently
hi,
Although i am not a web programming guru, just trying to solve ur problem with my little knowledge.try this code...
<BODY onbeforeunload="call_a_function()">
we need an onbeforeunload event that fires prior to a page being unloaded.
set ur flag variable to 0 using that function.
If u need the code i'm there. But i think u should better use session concept rather using a flag variable.
Put a new field in your users table (the one that has the flag which tells if the user is online) named session_time or similar. Every time the user goes to a page, update that session_time field to the current time. For an example on how to grab all of the users who has been online in the last five minutes:
$online_time = time() - 300 // 300 = 60 seconds x 5
$sql = "SELECT * FROM users WHERE session_time > $online_time";
Optionally, you could add " AND flag = '1'" to that sql query, and it wouldn't select anyone who had hit the logout button before closing their browser.
check out the garbage collection of session_set_save_handler (i.e. session times out and you define what's going to happen, i.e. set user_flag to 0 or whatever else you need).
Mr feyd
the problem is that gc functions does not execute
i set the maxlifetime to 5 seconds in php.ini
but no execution
When this function execute exactly
The function will execute based on your php's settings. session.gc_probability and session.gc_divisor set how often php will call the garbage collector.
For example, the defaults 1 and 100, respectively, means PHP will call the garbage collector once per 100 sessions.