0

I'm trying to figure out how one would detect whether a web-page is in use. I am building a kind of web-appliance which has an HTML5 RDP client type thing. I need to detect whether the page is currently in use, if possible a specific url including a GET variable. I have searched every variation that I can think of for doing this and have come up with the following, but none seem to work for me.

Pretty much my goal is to make a change in a mysql DB whenever a the page is active, then change it back when it's not.

  1. PHP sessions. This won't work for me because the session may still exist even if the user has left the specific page (I believe).
  2. Javascript window.onbeforeunload, however I see that this is pretty unreliable. Particularly if a user has security features enabled.
  3. I found some obscure referance to using php enviromental variables, but I wasn't sure how that would work, since it still wouldn't revert the variable or dismiss it when the page was closed.

Any suggestions are greatly appriciated!!!!

Ethan
  • 787
  • 1
  • 8
  • 28
  • 2
    Give it up. You can tell a page has been read. Once it's on the client you can't tell what's happening. Is the page active if it's open in the browser but the user has gone for lunch? What if the power goes off suddenly? What if (any of a dozen other reasons you don't get a notification)? The best you might be able to do is use Javascript to send a heartbeat and run a daemon on your server to clean up every few minutes. –  Oct 01 '14 at 00:32
  • maybe something like this: http://stackoverflow.com/questions/3877821/how-to-keep-alive-a-users-session-while-they-are-posting-on-a-forum – manishie Oct 01 '14 at 01:02
  • So, I'm okay with a user leaving his/her session open for a long time. I essentially just want my property "active" in mysql to be changed to "available" if there is there are no users on the page. Really I don't care about what they're doing or seeing, I just want to change a property if the session isn't open anywhere :S – Ethan Oct 01 '14 at 01:58
  • I like the jquery suggestion Manishie, but I don't know that it'll work since it won't change the value back if the page isn't in use. What would be perfect is if there was a way to make a kind of temporary variable that is only accessible if the page is in use, or something similar... But so far as I know there's no way to do that. :S – Ethan Oct 01 '14 at 02:08
  • Maybe initiate a long polling or websocket when page is loaded, and monitor the connection. Actually it depends on how you define _in use_ for a web page. – rhgb Oct 01 '14 at 02:14
  • Hmmm I'm not sure how to poll it though since there doesn't seem to be any property on the page which will tell you "yes, someone is viewing this page" :S – Ethan Oct 01 '14 at 02:17
  • Your best bet is probbaly something like [jQuery Idle extension](http://plugins.jquery.com/idle/). It supports 4 events, idle, active, show and hide. You just have to add a call back to each of those functions. You will also need the `window.onbeforeunload` event to tell the server that the user is leaving the page. But like @MikeW said if the powers goes off you are screwed! – ForguesR Oct 01 '14 at 03:23
  • @Ethan How would the variable(s) be stored at server ? / processed by php ? i.e.g., if there were 1,000 simultaneous unique viewers of the same page ? Thanks – guest271314 Oct 01 '14 at 15:20
  • Yeah I think what I want isn't going to be possible unfortunately... And @guest271314, that's pretty much what I was trying to figure out :S But no, I pretty much only want one user to be able to view the web-page at a time. – Ethan Oct 01 '14 at 19:09
  • If only one viewer at a time , may be possible utilizing js web workers , php and a text file . Closing Question ? – guest271314 Oct 01 '14 at 20:57
  • Alright, yeah that's fine. Go ahead and close it. Thanks for your suggestions all. – Ethan Oct 02 '14 at 15:39

0 Answers0