views:

82

answers:

3

I'm not really sure how I would implement this along with a fair amount of security. I want to be able to know if someone or two people are on based on a mysql row. These two people are totally anonymous. I do have an xmpp server set up but I'm not sure how that would integrate into the mysql and I already have a separate login system for it. Server side is done with PHP

Edit: I want to ONLY allow two people on a certain page and that is it. But, if they leave the page I need to determine if they have left.

+2  A: 

The issue is that if somebody closes their browser you have no way of being notified that they have left the page.

What you could do is have an AJAX request on the page, so that every 10-15 seconds it sends a request to the server containing a session id. Log the timestamp and the session id in a table, and you can then query the database to see what requests have been made in the last 15 seconds

waitinforatrain
I do have xmpp setup so I don't have to do polling. I was wondering if there's another way without hammering the server
+1  A: 

You could try a solution using the OnBeforeUnload event, to notify you that a user has left the page, see best way to detect when user leaves a web page.

David_001
So what happens when the browser crashes or when the connection goes down?
@user434366: you can't do that without some form of callback (eg. ajax) telling the server every x seconds that the user's still on the page - which is fine as a solution. I've just pointed to an alternative solution which doesn't require polling, but would need some for of session timeout (to detect that the user hasn't done anything for 5 minutes, for example).
David_001
ok seems decent ill consider it
A: 

When you say "page" you are talking about browser-side-validation, because browser should be able to tell you that the user is still on that page. If you are trying to do this then you need AJAX like waitforatrain's comment. But there are some loopholes:

1) what if browser has javascript disabled?

2) what if user is inactive after loading the page? Should the page be kept "alive" ?

Ashwini Dhekane