Automatically Log-Out Users When They Close Their Browser - php

I'm using PHP as server side language and Flex as Front end. In my application there are different levels of users. According to operator's online status QC assigns some job online. I just want to update user's online/offline status when he closes the browser automatically. Flex calling PHP service but its not getting any response. DB(MySQL) is not getting updated. Any Help is appreciated.

As far as I know this is not possible. Your best bet is to save the datetime in the database for the user when the they log on and then run an AJAX script to update that user's time each minute in the database. Then you can check to see if the user has a datetime older than a minute to see if the user is logged in. You can adjust the "resolution" of this approach... checking every 5 minutes for example.

If you are wanting a service call made before the browser closes, I think you will need to have Javascript send a message to your flex/flash client, then have flex make the service call to let your server know the client/browser is closing. Once the service call (request/post) succeeds, flash client responds to Javascript to continue browser close. Caveat here is that I don't think you can consistently have javascript block from browser to browser. The browser may close before flash can get the service call initiated. Good luck.
BTW, you can register a call back listener using ExternalInterface and have javascript signal Flex using the javascript function referenced in the callback listener.

Thank you all. I was confused about AJAX but finally its done. I'm calling a function while closing browser. <body onunload='Logout();'>. Here's the function
var xmlhttp = new XMLHttpRequest();
url = "http://*****.com/public/LogOutThruJS.php?UserID="+UserID+"&LogHistoryId="+LogHistoryId;
function Logout()
{
xmlhttp.open("GET", url,false);
xmlhttp.send(null);
xmlhttp=null;
delete xmlhttp;
}
I hope its understable :).

Related

I need to push data to browser of all clients which are currently browsing our web page

We are making a app with php in which we want to push data to client browser same in the way in which facebook notificaiton sent by facebook. i know about commet and also have used ajax in past but ajax is not efficeint while commet programing is out of my mind.
Sample can seen at ESPNCRICINFO.COM live scorecard which is automatically pushed
by server to brwoser and than append to document without refreshing. Same app we
have to made.
Simply i need to build some code that send data to all the browser which have our webpage opened. No restrictions. Just need to send to all. So there is no need to check to whom data will be push.
I really need it urgently.
can you use JavaScript setTimeout(function name, seconds) ?
In JavaScript function you can generate .post request for some PHP script which can load messages, when the message will load, you can show it via you JavaScript. the timer will call this function for all people.

how to detect user is disconnected via PHP?

Is there a way to detect whether a user is disconnected from internet or not? The way that stackoverflow does when you want to post a question. I couldn't think of any approach to do that. Could someone shed some light on the subject?
You can send AJAX-request to PHP script when windows is closed:
window.onbeforeunload = function(){
// Request goes here
}
Alternativly you can use websocket-technology (you can use phpDaemon) to connect with server permanently so you will know when user is disconnected from internet or your site or pereodicly (use setInterval function) ping your server.
I guess Stack Overflow uses AJAX, which is a JavaScript driven program executed on the client side inside your browser. This ajax setup is responsible for notifying the user when, for example, a new answer is posted, and giving them the opportunity to load said new answer without reloading the whole page.
And this construct has a way to detect errors in the communication with the server which it is interpreted as the user being disconnected, resulting in a warning.
However, this requires that the user is still having the browser open. There are also various other functions in JavaScript and AJAX to execute something when the user is closing the page, but neither of them are considered to always work. There are no silver bullets after all.
From the server's side, one can monitor the constant ping-pong of the user client's AJAX and execute something when this ping is fading away. Like: the user has been pinging us in every 5 second in the past two minutes, but now this ping is missing.
The main problem with this lies inside the principles of PHP and that every pages basically lives on its own. When the request is get, the page is loaded and created, but at the end of request, the current page instance is denied from existance, just like how every variable is lost which is not saved elsewhere (cookie, session, database).

Using XHR progress events to monitor a MySQL query

I have a php script which is called by a javascript XHR function. In this script it accesses the Facebook API using a token (which is parsed from a Facebook redirect after accepting to connect to facebook).
When I add a progress event to the XHR object, it never runs, and when I log the readystates it gets to, only 2 and 4 are called. I don't think its necessarily a problem with my code, my only guess is that in my PHP script it gets the file contents of 'https://graph.facebook.com/me/albums?limit=0&access_token=' + $token (which json decodes its contents to use as an object which is looped through, adding each of the users' album(s) to a database) doesn't like the fact that I am visiting another url inside this XHR request.
Can anyone give me some guidance so I can make my progress bar 'load'?
Thanks!
The best way to do this is probably to just use good old readyState

How to refresh the Server or Database Table Values for every X Seconds

Ok i am tryin to make a Chatting application where users can chat with each other if theyr logged in to the website. I have set up everything except a little working which i am not able to write.
In my example : 2 users are chatting with each other : User A and User B
When User A submits a comment in the Chat Window , then the whole div containing the Comments should be refreshed not only at User A's side but also at User B's side so that when User A submits the comment, User B should be able to see it.
In short refreshing the div on every users page when a new record is added to the database table.
Im good in PHP, Jquery, AJAX.
Every x seconds use Ajax to check for new messages and if there is a new message, write it in the div.
There is no way for the server to tell the client a new message was posted. The client has to ask the server.
If you're comfortable restricting your users to new technologies, your best bet is to look into HTML5 websocket API. It's still a bit green around the gills, but it's certainly the way these things will be done in the near future. Kaazing's default example is a chat client, actually.
On the server side, there are implementations for websockets in JavaScript (Node.js), Java, and other technologies, but I'm not sure about PHP.
If you don't want to use websockets, research techniques that fall under the name "COMET" or search for "long polling". These will be far more responsive than a time interval.
If on the other hand you don't need it to be super-responsive, you could just use setInterval to have both clients poll the server for changes.
DC's answer is correct, if you are looking for an AJAX polling solution. This could be done with a simple JavaScript Interval.
However, polling is not light on resources as there are going to be a lot of duplicate content requests - and you would need to poll frequently to make the chat feel as though it is real-time.
If you would like to push data to the client from the server, I would look into writing this application using web sockets. NodeJS would be perfect for what you are trying to achieve, depending on what browsers you are wanting to support.
you need a timeout function like so:
function updateChatWindow(){
var xhr = $.ajax({
url : /* controller url */,
})
.done(function(response){
/** do something with the response the server gave you back **/
});
.fail(function(response){
/** handle a lost connection is some way, maybe show a message to the user, or retry request */
})
.then(function(){
periodicRefresh();
});
}
var timeoutFunction; //idealy, this will be inside a javascript object
//starts a refresh (in 500ms)
function periodicRefresh(){
timeoutFunction = setTimeout(updateChatWindow,500);
}
//stops the refresh from happening, see below why
function clearRefresh(){
clearTimeout(timeoutFunction);
}
var inputElement = $(/** the input area where the user types text **/);
inputElement.keyup(function(e){
if(e.keyCode==13){ // or whatever the e.???==13 was to check for enter
clearRefresh();
/** submit new chat line **/
updateChatWindow();
});
updateChatWindow();
Recap:
1) Have a ajax call that will update the window
2) Have a timer function that will store within it a timeout variable that will trigger a refresh of the content (the ajax call from 1)
3) Have the ability to cancel that request (because users type text, and after they hit enter, you should refresh the chat window no matter what)
4) Have the ability to stop the timeout function somehow to allow 3) to happen.

Server Side Reload Initiation on Ajax Call

I need to initiate a reload from the server side(PHP) durring and AJAX call. How do I do this. I honestly don't think it is supported.
My current method is to use the PHP header() function and capture it as responseText from the AJAX call and then do an innerHTML add.
However it is not a proper reload in my book as it doen not intiate the onload function. It also seems to to be contrived and not a "clean" solution.
Send a command/flag back to your Ajax handler to make it call the following JavaScript method in the page:
window.location.reload();
There is a pattern that uses AJAX to create a PUSH-alike application. I don't know the real name of the paradigm, nor a good implementation, but this is basically how it works:
Client makes AJAX request to server.
Server doesn't close the connection.
When server wants to push. server says:
a. Process push message X
b. Initiate a new connection. (i.e. goto 1.)
< Server closes connection >
So there is always a "hanging" connection, and as soon as the server wants to respond, the client immediately creates a new one that hangs again.
The only way to reload the entire page is to change the window.location. You can't do that from the server. Every action must be initiated from the client - that's fundamental of restful protocols like HTTP.
However, your server could send back the entire document, and you could then 'pick' the portion to replace. jQuery allows this with the load function.
For instance this would reload the BODY:
$('body').load('ajax/page.html body');

Categories