3 Replies - 232 Views - Last Post: 21 January 2014 - 04:40 PM

#1 Lieoften  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 243
  • Joined: 06-January 10

Multiple window detection/prevention

Posted 19 January 2014 - 02:30 PM

Alrighty folks, So i'm currently working on a project that is half Jquery half PHP half me being bored out of my mind. And i'm having a tremendous amount of trouble in trying to detect/prevent multiple windows from being open--or if that's not possible to at least prevent the php file from running twice for a user.

My current method of trying to prevent multiple variants of the code running is this
if($_SESSION['time'] == time()-2)
{
$_SESSION['money'] = $_SESSION['money']+ 1;
$_SESSION['time'] = time()+2;
}
else
{

}



Which does not work at all--though I think that might be a problem with my test server as the time goes up by 2 every time the code runs.

anyway, I'm posting this here because I don't think my PHP solution would be the best and I'm looking for any solution in any language.

Is This A Good Question/Topic? 0
  • +

Replies To: Multiple window detection/prevention

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,025
  • Joined: 08-June 10

Re: Multiple window detection/prevention

Posted 19 January 2014 - 11:25 PM

actually, thatís not possible from the PHP side. PHP receives a request, processes the script(s) and then returns the response. whether those requests come from the same window or not cannot be determined by PHP. thatís the problem with a stateless protocol like HTTP.
Was This Post Helpful? 0
  • +
  • -

#3 Lieoften  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 243
  • Joined: 06-January 10

Re: Multiple window detection/prevention

Posted 20 January 2014 - 09:11 AM

Yah I found that out this morning after like six hours of searching the hell out of it. I've resorted to java to fix this problem and as of right now it seems to be working perfectly.

This post has been edited by Dormilich: 20 January 2014 - 09:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 318
  • View blog
  • Posts: 1,173
  • Joined: 15-January 14

Re: Multiple window detection/prevention

Posted 21 January 2014 - 04:40 PM

Depending on what your requirements are, there might be some things you can do. For example, I have an application where I use cron jobs to schedule certain scripts to run every minute to do whatever work needs to be done then, but I never want more than one instance of that script running. So, if the code starts up and a previous instance is still running, the new instance should just quit. One example would be processing a mail queue, if it takes more than a minute for the mail queue to be processed I don't want to start another one which would start processing the same queue with the same mails.

I ended up using register_shutdown_function to define a function that should run whenever PHP quits (even if it was a fatal error that caused it to quit). The shutdown function would reset a flag that the script previously set when it started. So, the script starts and checks the flag (the flag could be stored in a database, or text file, or whatever). If the flag indicates that the script is already running, then the new one just stops. If nothing else is running then the new script sets the flag, and registers a shutdown function to reset the flag. So no matter how the code ends, the last thing it will do is reset the flag to indicate to the next instance that nothing is currently running.

http://www.php.net/m...wn-function.php
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1