I have a user status field (online/offline) in database. when user hit logout button at that time in database change online to offline. but if user directly close browser without hitting logout then how to update database.
Unless you have a process which pings your servers at a regular interval, you can't.
Your logout button most likely sends a request back to your server. Upon receiving that request, the server runs/delegates logic to update the DB. If the user closes the browser without clicking the logout button, the server never gets this request.
#Antony gave a possible solution.
Another possible solution would be to send a message to your server at a given interval. The server should expect this call. If the server doesn't receive the message, then mark the user as being logged out. It has the downside of the logout timestamp being off by the interval. The logout time will not be exact. See this thread for more detail Ajax call with timer
Edit #1
In the link I mentioned above it mentioned how you can run javascript code at an interval.
setInterval(function() {
//call $.ajax here
}, 5000); //5 seconds
In your case use this function to call your API.
Your API should record timestamps of when it last received a call from your javascript. Have a cron job, or another piece of logic to check when the recording for that particular user stops. The last timestamp would be the approximate time the user logged out.
It's a very involved process for simply tracking a user's logout behavior. You may want to consider if it's worth the trouble.
Related
I have an app where a user absolutely must be logged in to use it.
I use PHP sessions upon login to store user data.
If a user leaves a window open and then returns e.g. an hour later, what's the best way to check if they are logged in still?
Options I can think of are:
1) check mouse movement which then fire AJAX php file to see if session still active
2) check as above on mouse click anywhere
But these will then be running constantly and therefore hitting and hitting the server potentially thousands of times a second with not so many users, so I doubt efficient.
Is there a better way to do this? Is there a 'standard'?
I am using PHP and JQuery.
Use Javascripts setInterval(function, time) function to poll the server every X number of minutes to check if the user is authenticated. Return a response in JSON based on said result.
See javascript setInterval for a good discussion about the setInterval() function.
If you are in fact asking about a user being active on the website, you could use the setTimeout() function after X amount of minutes (such as an hour) to log the user out via AJAX request, or display a warning such as the ones in online internet banking.
If a user makes a request before the timeout occurs then this process is restarted naturally due to the request - response cycle.
Basically I have a chat, using AJAX, PHP.
I let guests login by filling their name and clicking login.
It fills the session $_SESSION['guest'].
Now, when they close their browsers, or something, once the session ends, I want it to say in the chat
"The username has left the chat".
But I am not sure on how would I do this.
Any ways to do it? Maybe I could fill an array of users that were active in the past 5 minutes, if not, remove from the array and it will kick them off the chat.
Any ideas?
You could have an array of users in the backend with the timestamp of the last ping.
Then, the clients end can update the backend using ajax by sending a new timestamp every so many seconds.
Whilst your backend is being updated by one of the users, your backend can do a check on all users checking the last timestamp sent to the current time, if its over 2 minutes or something you can tell they have left the chat and output the message.
Without a direct connection to the browser, you won't be able to actively tell if they're connected. I'd suggest a ping-like solution, requiring the browser to ping the server every n seconds to keep the chat "alive".
If the browser doesn't "check in" with the server, the server assumes they've disconnected and reports back to the other participant.
That said, if you're able to dive a little deeper, Google "javascript real-time chat" and you'll have several paths to success.
Add a jQuery .unload() event to your chat client. When they navigate away from the chat it will be fired and you can use it to send the last bit of data to display that message.
Here is the documentation: Link.
Example:
$(window).unload(function() {
//send message to server informing it of client leaving
});
I have a situation where I need to run some PHP, specifically where I need to send out a SOAP request and wait for the response, and then do something with that, however sometimes these requests can be slow and take up to 9 seconds.
Now I don't really want the user sitting there waiting 9 seconds for this to complete.
Basically the user flow is..
User comes to payment page
User clicks button to pay via payment gateway (Paypal)
User then returns to the site (SOAP request and all that need to be finished at this stage)
I was thinking of running it with the Paypal IPN notification but then didn't think it would be finished by the time the user got back to the site.
So, I'm wondering if I could send off a call when the user hits the first page via Ajax and have it run whilst the user is submitting payment and by the time they get back to the site it should be done -- it's not a big deal if they don't end up going through with payment, so I'm not worried about running this code before confirming payment.
My question is, if I fire this off to be run via AJAX, will the code still be executed if the user leaves the page before it has finished? If not, any ideas?
Once a request is sent to the server, irrespective of whether you navigate away from the page the server side of the request will get completed.
The only thing that will not happen is the execution of client side callback method.
If you are using php , there is a php.ini setting ignore_user_abort that tells php what to do when the client aborts the request.
Its value is false by default.
http://www.php.net/manual/en/misc.configuration.php#ini.ignore-user-abort
Status as in online/offline..
When a user enters onto a page, it records the current time and puts it in the database.. if 5 minutes passes since their last action, it shows them as offline?
You can use JavaScript to time the five minutes, then use AJAX to call a PHP script that updates the database.
window.onLoad = function(){
setTimeout(UpdateDB,1000*60*5);
}
function UpdateDB(){
// AJAX call...
}
This would be a good way to start. You can easily check the period of inactivity.
It would be relatively simple for you to set up an AJAX ping to your server on a long interval~ so that as long as the user's browser has your site loaded, it's still pinging it's status as online.
I'm writing a web application that autorefreshes data with an AJAX call at set intervals.
Because it's doing that, server side user sessions never time out, since the last activity is refreshed with every ajax call.
Are there good client side rules I could implement to time out the user? I.e. should I track mouse movements in the browser, etc., or should I point the AJAX calls to URLs that don't refresh the session?
I like that my AJAX calls hit a session-enabled URL, because I can also validate that the user is logged in, etc.
Any thoughts in terms of whether I should even bother timing out the users?
One technique I've used: increase the interval between AJAX calls every time a call is made. So you make your first AJAX call after 10 seconds, then you wait 11 seconds, then 13, then 16, 20, 25, etc... (or some similar pattern). Every time there's page activity (found by registering some JS event), you reset the interval back to your starting value (e.g. 10 seconds).
This technique will cause users who don't touch the browser for a while to time out eventually, when the AJAX interval becomes longer than the timeout period. As an added bonus, you'll r educe your server loads -- if a user leaves the browser window open for a long time, they'll make fewer and fewer requests before timing out.
I prompt the user to verify they're still active via JavaScript after a period of inactivity. Inactivity is defined as "no mouse or key messages sent to the window". If they fail to respond to the prompt after a certain amount of time, I redirect to a sign-out page.
My jQuery UI-based implementation can be found here.
I've done this by maintaining a "last action" timestamp in the browser and sending this back to the server with the heartbeat. On the server I then check for a timeout based on the difference between this value and the current time, calling the logout routines if the user has been idle too long. If the session is timed out then heartbeat result will trigger the browser to reload the page which, as the session is now logged out on the server, will clear any user specific information.
The two main problems I had to solve with this approach were differing interpretations of timezones in the server and client date function implementations and keeping track of the most recent action if the user had several tabs open in the same browser sending different "last action" timestamps back to the server.