Is that possible?
<strong>For curious people</strong>: I need this behaviour because my app uses some session data to identify sequences of actions. If the user opens a new window on the same browser, normally everything is right, but if he/she starts a new sequence, the previous one (in the other window) will be lost.
While it is correct what all others said here that you cannot prevent someone from opening your website twice, there might be a workaround for you. Note that this is not a 100% solution and will require you to develop further code based on this.
As far as I understand you have the following problems:<ol> <li>How to determine if a user has your page open with a window/tag that shares session data (i.e. two tabs in Chrome, 2 Chrome windows etc.) - that is the easiest part though.</li> <li>How to determine if a user has your page open in a different browser (i.e. open in FF & Chrome at one time) - that is the tricky one.</li> </ol>
However - you could use LSO - Local Shared Objects to accomplish this. These are Flash based cookie similar data pieces stored at the client's side. AND THEY ARE CROSS-BROWSER & CROSS-DOMAIN COMPATIBLE. You can find a nice library called EVERCOOKIE for using them here.
By setting such a cookie you will be able to trace the user even if he opens a new window or even if he opens your page in a different browser, as far as the browsers share the same Flash installation etc.
Note that this will not prevent someone in opening two windows directly, but it enables you to send him to the same sequence state as he is in the other window for example.
You should also be aware that this is not a 100% solution. There will always be some trick to get around this, although I think normal users would not be able to get it...
You <strong>cannot</strong> prevent someone from opening your website twice.
You might by able to use a new session key for every page, but that can get a bit hanky...
As mentioned above — you cannot prevent someone from opening your website twice. Yet your problem is, as I see, to actually mare sure that every browser session is unique. There are many approaches to reach this, one could be to generate a random number by the client and send it via ajax to the backend to be used instead of a session-id, you might have some persistence problems, but these are all easy to tackle.
The only way I can think of doing this is to hide / remove content if the user is logged in somewhere else
So you would a) need the user to login ... and b) record when the user logged in ... and c) record when the user logged out
There are still problems with this method - when do you decide when the user has closed the browser and should be removed from the "logged on" list ...
In short the answer is you cant reliably stop this ....
I would do it using sessions on the server side (i.e php) , and utilize the load and window.unload events to set and destroy sessions.
If a session is already created, then any window that opens up after that , should check for the session value, and force a close after showing an error message.