Avoid opening the same page at the same time.

I am trying to find a solution to this problem: I have a page in my php application that cannot be opened several times to avoid overwriting the session. I spend a lot of time finding no solution!

The last thing I tried was to use the jquery "load" and "unload" events in the window. It works great if I open the same page in a new window or a new tab, but also blocks the page if I refresh it!

I really don't know how to do this! I thought this was a simple example of using a semaphore ... but I cannot encode it correctly.

Any suggestions? (both php and js solutions are welcome)

+4
source share
4 answers

You can try with a cookie:

  • First page load, cookie set (server for session)
  • The next time the same page loads, it reads the cookie
  • If a cookie exists, do not overwrite session variables.

I do not think that the browser will be able to change the difference between downloading and downloading in another tab / window.

+1
source

It is not possible to open once by the same user, I suppose.

How did you use jquery loading and event unloading? Brainstorm here, how about using load to install a semaphore in a user session that is removed using unload . Of course, I'm not sure if this event fires even in the event of a browser crash or similar unforeseen technical problem. In this case, you may be stuck, for which you need a certain timeout, or wait for the session to exit.

To fix this, it is possible that after loading the page there is a timer that runs every 10 seconds, basically updating the semaphore view like "Page is still open" along with the current timestamp. If a semaphore exists at boot time, but the timestamp is older than 10 seconds, the user is updated.

0
source

The problem is that you do not have access to the end user pages that are currently open in the browser. Thus, the only solution is to track the pages that the user has opened / closed / switched to another page / etc. Even this is not ideal, as the user can open another browser. Therefore, there is no perfect solution.

0
source

Sorry guys ... you're right! A little code will help us all :) suppose my script is script.php

in html script.php i put

 $(window).bind('load', function(){ $.post(PATHTOLOCK.php); }); $(window).bind('unload', function(){ $.post(PATHTOUNLOCK.php); }); 

In the PATHTOLOCK.php file, I do this:

 $_SESSION['flag']=true; 

And in the PATHTOUNLOCK.php file, I do this:

 $_SESSION['flag']=false; 

At the beginning of script.php I will put

 if($_SESSION['flag']==true){ echo "error";exit; } 

If I open script.php in two windows / tabs, everything works fine. If I refresh the page, this will not work, because I assume that the sequence of events is as follows:

  • click on update
  • the unload event is not raised because the page is not completely abandoned
  • script.php reloads
  • check at the beginning of script.php is not performed because flag = true
  • script.php goes about error
  • the unload event is dispatched because the page is left and the flag = false
  • click on update
  • now it works!

etc .... every two updates it works!

0
source

All Articles