How to handle user online status when he/she close the browser? - php

I am having table to track user online status by setting 'is_online' field to 1. Now User get logged in the site and shut down his/her system or power down at that time he/she logged in.
Here in database 'is_login' has the value '1' which means he is still using the site.
So I have to do some logic to change that value to '0'.
How can i handle this situation using PHP ?

The normal solution is to store a timestamp in the table which you update every time the user does something. Users with a recent timestamp (say, five minutes) are shown as logged in, everybody else are logged out.
It doesn't even have to be updated on every page load. Keep a timestamp in the session with the last update time as well, and only update the table when the database flag are about to expire.

Store an time-since-last-activity. When it's been longer then a specified time - treat as if offline. You should replace the is_online with an DateTime field, and update it every time the logged in user visits the website.
On the place you want to select all online users, instead of :
SELECT * FROM users WHERE is_online = 1
You could use:
SELECT * FROM users WHERE is_online >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)

you can't. HTTP closes the connection once the page has been delivered. On the server side alone all you could do is checking if a user has requested a page within a certain amount of time.
JavaScript might be your friend. You could use it to create a ping to a php script which then updates the last request timestamp. Once there is no more pinging, the user is gone and you'd know.
If you have trouble with javaScript, a good point to start would be here: http://www.w3schools.com/JS/
You might want to focus on the AJAX and HTTP request stuff.
cu
Roman

You can use jquery to update your site each X time, so a function is executed each 10 seconds, as following code:
function updateTheBBDD(){
... calling mysql_connect -> mysql_insert -> mysql_close
}
setInterval("updateTheBBDD()",10000);
Also, when painting the user status monitor instead of consulting the BD and:
1->green
0->red
You should:
actual - time > 10 ---> red
actual - time <= 10 ---> green

Due to the stateless nature of the web it is not possible to know exactly when the user left your website. So you can't really trigger any code to change the mentioned value.
As the other have said one has to store the time-since-last-activity to get an approximate idea when was the last time the user was actually doing something with the website.
HTH :)

Related

Update information at specific timestamp

I'll try to explain my question the best way I can.
I'm not asking for codes, only for the best method of doing it.
I want to create a browser game and use time for upgrading stuff, building etc.
For example, to build 1 house will take 1 hour.
So I will began with saving the timestamp+(60*60) at the moment the user did his action.
My question is, how to update it the best way?
One way I thought of was to add function that check every page view of the user if it's done.
But then if he's not logged in the update wont happen.
Second way i thought about is for every page view of any user to check for every user registered. But it's not effective and there is a problem if no user is logged in.
Any suggestions?
I had my game doing it simply, without crons.
When a player wanted something that takes time, i just updated his database information with the appropriate time of ending that job (columns are just examples)
UPDATE player SET jobend = UNIX_TIMESTAMP() + (60*60*4) # ending in 4 hours
Then, every page that had an information about the remaining time, i just used something like this:
SELECT (jobend - UNIX_TIMESTAMP()) AS jobremaining FROM player
I formatted correctly the time using strftime and i displayed that to the user.
In the case the remaining time was negative, the job was done.
There was no-need for absolute counting since user was able to do something with the job when he was connected.
When the player just changed pages or doing something else i had a function where i just checked all timely events while the user was online (so to catch any negative timer), then presented with javascript any change (i posted javascript counters for every page)
Now, if you talk about updating in real-time, cron is the way but are you sure you're going to need it for a game? I asked that question myself too and the answer was not.
EDIT
If another player sees the buildings on schedule page (an hypothetical page) i am doing the same calculations; if a time just got negative for a specific player (regardless if another player see the page), i just reward him with the building (in database i make all the changes), even if he's offline. There's no harm in this, since he can't do anything anyway. The other players will just see he has a building. The key here is that i execute the required updating PHP code regardless of player's connection to the game; as long at least ONE player is logged-in i'm executing the progress function for everything.
This isn't so slow as it sounds (updating all players by using just a connected player that visits a specific page). You just have a table of 'jobs' and check timers against the current time. More like a single query of getting the negative ones.

how to check user is online or not in real TIME

I'm currently trying to display all online users on my SITE'S userpage using the php session variables. To do this, whenever a user logs in or out, a column in a database gets set to "1" or "0".. However this doesn't entirely work since the database doesn't get updated when the user closes their browser (and therefor destroys the session). So is there another way of checking if a certain sessionid is set??
I also want to know how the twitter and facebook handle this ?
You almost have it. The way that the vast majority of sites deal with the issue is to have a table like you do, but they add a timestamp to it and update the timestamp when a new page is loaded.
When querying the table, you simply look for say the last five minutes of active users and they are the "live" users on the site.
Technically, you don't even need to keep the "logged in/out" value in that table. If they have been logged in within the lat five minutes, they are probably still about.
There is no guaranteed, sure-fire, totally bullet-proof way of checking if a user is there or not. You can do some tricky JS to ping on and off, you can add even more JS that will try to alert the db when the user navigates away from the page - but at the end of the day, you cannot do anything if a browser is closed unexpectedly, or if that user loses power, or network.
On top of that web browsing is by default stateless and doesn't maintain a connection to the user after the server has finished sending code. The best we can efficiently do is update a table when the user does something new and assume they will be around for a few minutes at least.
I haven't checked but Twitter and Facebook most likely have Javascript code which notifies the server when somebody closes the page, probably coupled with a periodic heartbeat and timeout.
Check the onunload event and XMLHTTPRequest to see how you can make a request to your PHP application notifying of an user leaving (a library like jQuery might help you do this much more easily).
Add a field "last_visit" to user's Table and update it every time when user visit your site
When user login to your site find "last_visit" time and current time,after that use this function
$time_period = floor(round(abs($current_time - $last_visit)/60,2));
if ($time_period <= 10)
$online_offline_status = 1;
else
$online_offline_status = 0;
}
and then print your final result
<?php if (
$online_offline_status == 0){ ?>
<span style="color:#FF0000;">Offline</span>
<?php } else if ($online_offline_status == 1) {?>
<span style="color:#669900;">Online</span>
<?php }?>

How To Write To Database User's Logout Timestamp When Browser Is Closed / Crash

I have a table on my database called 'Log'. This Log table contains ID, Username, LoginTime and LogoutTime. That's all.
It's really easy to INSERT into ID, Username and LoginTime columns...
But dealing with LogoutTime, I was thinking there will be 2 condition :
User click logout button >> PROBLEM SOLVED, I can easily put INSERT script on my logout.php to handle LogoutTime column.
User just run away from my website by clicking close button of their browser >> THIS WILL CAUSE A PROBLEM. How can I write to LogoutTime column in this situation like this?
can it be handle by PHP only? I mean, doesn't have to use jQuery... Thanks before.
The most common solution to get this done is by using a timeout. So you run periodically a cron which checks whether there are users which are not active anymore. For example it can be no requests done for the last half hour. Then add the logout time at that moment.
So every action update the user session with the last request time so you know when they have a timeout.
Controlling 100% a closing browser is not possible. Think for example about a power failure at the client computer, you will get nothing back for sure.
You can implement something similar to this, the time can be different according to your need
if (isset($_SESSION['browser_last_activity']) && (time() - $_SESSION['browser_last_activity'] > 1800)) {
// request 30 minates ago
// set the logout time here
}
$_SESSION['browser_last_activity'] = time(); // update last activity time
But there is no way you can implement functionality 100% meet your requirements with php.
And with javascript you can try with onbeforeunload, it is good practice than do it with javascript and not in php because of overhead reasons.
In IE you can try something like this:
< body onbeforeunload="alert('Closed!');">

Tracking how many hours users spend on my website

I am writing a usersystem and as a statistic available for all my users I want to be able to provide them with the amount of hours they've been active on my website.
my original idea as to how to do this was:
user loads a page, and a record for the user is stored in a SQL table called accessLog
*user loads another page, sql query runs to see when they last visited a page*
if the user visited a page within the past two minutes, update their user account and add the time spent online
*insert a new record into accessLog*
however this would mean adding a potential of 4 additional queries to every single page of my website, which seems slow. I was thinking, there has got to be a more efficient way of tracking user activity online that I'm clearly too stupid to see!
My way of doing this is simple:
When a user loads a page, their "last page load time" is updated in the database.
A cron script runs every minute. It looks for users whose "last page load time" was in the last minute.
Increment the "time spent on site" for each user found.
In this way, I can keep track of users accurately to the minute. Just divide by 60 to get the hours.
I would use ajax to get the clients to call the server ever minute or so. Just a to tell the server that the user is still watching that page.
The server would have a lastTimestamp and a totalTimeSpentOnSite in a database. The lastTimestamp would simply have the last time a ajax request or page load occurred.
Every time the ajax fires the server would calculate now - lastTimestamp and add that to the totalTimeSpentOnSite.
To make it even more accurate you could
add javascript listeners to onBlur() to record only the actual time spent with that window active and then send that time to the server instead of just telling the server that the user is still active.
My philosophy is this: don't be overly concerned with efficiency until it becomes a problem. Unless your site has really heavy traffic, you won't notice the four queries. This might be simpler, though:
Log every page load.
Every few hours, run a script through cron that calculates time
spent online for each user and updates a running total in the user
table.
Periodically clear out your access_log table.
Add it to your session management. When a user starts their session record their start time. When the user ends their session record their end time. Simple math from there will gather you the total amount of time they are spending per session on your site.
To answer Filip Haglund
yeah but you'll have to wait for the session to time out, and that time doesn't reflect the actual time the user spent on the last page before he/she closed it.
That's true but then again, all of the answers available so far work on the same basic premise:
Log the page load time.
Log when the user leaves
Update a database table
It just so happens that to log when the user leaves you need sessions. I don't know of any other way in PHP to keep track of user activities the way OP wants. Updating the database table is a technical matter. Do you wanna use a CRON job? Do you wanna use AJAX? Do you wanna do something else?
If you really wanted to know how much activity was actually occurring you could send off AJAX requests for certain JavaScript events. This will tie the time tracking to actual browser events. Of course this means your time tracking relies on JavaScript being enabled.
The "two minute" rule is a little unreliable - and you can get tied up trying to work out what "time spent" actually means.
In a previous project, we did the following.
On each page load, we checked if the user had a current session, and if not, created a GUID to identify the current session.
We wrote a log record to the database with the datetime, GUID, page URL, and user ID (if the user had logged in).
To calculate "time spent", we calculated the time difference between the first and last record for the session GUID, and added one minute to reflect the time spent on the last page.
This is not entirely accurate, but it can run realtime without the need for CRON for sites with moderate amounts of traffic.
first choose a duration beyond which a session is considered interrupted. (ex: 10 minutes)
log all page load with all ip in database
if time between 2 page load for one ip is more than duration don't add this time to the total time for this ip
if time between 2 page load for one ip is less than duration add this time to the total time for this ip
2 problem is :
- if user stay more than duration (10 minutes) in a page without refresh he's considered as disconnected.
- the last page duration is not saved
Piwik works like that
This is what i use and it checks updates the DB anytime the user loads any page.
The data is accurate, as it calculates if the user loads a page per minutes, if so, it adds the time difference to the DB.
$now= time();
$stmt = $db_con->prepare(" SELECT last_seen,time_online FROM user_online_log WHERE user_id=? limit ? ");
$stmt->execute(array( $user_id, 1));
$fetch= $stmt->fetch(PDO::FETCH_ASSOC);
$last_seen=$fetch['last_seen']; //integer value from db, saved from time() function
$last_seen_plus_one_min= $last_seen + 60;
if( $now < $last_seen_plus_one_min ){
$additional_time_online= $now - $last_seen; }
else{ $additional_time_online = 0; }
//update db
$stmt12 = $db_con->prepare(" update user_online_log set last_seen=?, time_online = time_online+? WHERE user_id=? limit 1 ");
$stmt12->execute(array($now, $additional_time_online, $user_id));

Logging Activities: How to record time using a PHP application?

I'm working with some existing PHP/MySQL code. I'm logging/tracking certain activities into a MySQL database. Certain access points are being logged. The number of times a user logs-into the system is counted. But, I need to also log the amount of time a user is logged-in, as well as the time the user is in a certain section of the Web site.
Since PHP is a stateless environment, for the most part, what's the best way to record the end-point(s); or when the user logs-out?
Note: I can't force the user to log out, as the browser can just be closed. Maybe I could just put up an AJAX timer that would count the minutes? If so, should I treat activities and time logged-in as different tables of information (MySQL)?
Edit: I forgot to mention we do have jQuery available.
Like you said, you can't force the user to logout, and you can't know for sure whether he's looking at your page or playing Pinball.
A solution would be an AJAX request every, say 5 minutes, to tell your application that the user is active. Unfortunately, if your user has locked his screen and went to play Pinball, you still don't know exactly what he is doing. Also, doing AJAX requests at intervals like this will increase server load, especially in a multi-user environment.
The best solution I think is to simply store the start_time of the user (when he logs in), then to update the end_time at every action he does, and with a session timeout.
Per example:
I log in at 5:00. Update the start_time to 5:00.
I browse to foo.php at 5:01. Update the end_time to 5:01.
I browse to bar.php at 5:03. Update the end_time to 5:03.
I go for a coffee at 5:05.
I come back at 5:15 and my session expired, I need to relogin.
So, you know I spent roughly 3 minutes on your application, since the last action I did was at 5:03 (5:03 - 5:00 = 3). Of course, you can't know exactly if it was 3 or 5 minutes. But you can assume, most of the times anyway, that if I don't do anything on your application (i.e.: execute a script, call, etc.), that I'm not using it.
Obviously, if you can capture JavaScript events like window close it's even better, or if I sign out manually: you update the end_time accordingly.
You need to capture two events.
The onCLose() event for the page and hook that into an ajax call back to your logging system.
The onClick() event for your logout button and hook it into the save ajax handler.
The onClose event will allow you to capture when either the tab/broswer is closed and the onCLick event is obvious.
Now this will not capture times when the browser dies, the machine loses power etc. so there will be instances where you will have gaps and those can be corrected by your login event handler and simply tag the last login event as logout out on the next login. This will however lead to outliers in your tracking of time spent logged in and you will need to statistically deal with those in your reporting.
You can use an extra PHP script that records the last activity and call it via ajax.
You can use javascript to monitor if the user is still active (moved mouse or pressed a key in the last 5 minutes etc.)
EDIT: Almost forgot the important part: your java script must make an ajax request eery X seconds.
So if there was no request in x+tollerance seconds you can consider the session as dead.

Categories