views:

436

answers:

5

Is it possible to detect if a page is opened in 2 different browsers?

I have built an small app, that stores some data in $_SESSION, and the problem is that when multiple tabs gets opened, the session vars gets overwritten (search filters in a search form) and so the tabs may display the same results for apparently different searches.

The main browser used for this app is IE, so it took a while to hit this problem and the app got ..larger and harder to modify.

Until i fix all the references to this search, i would like to do a "quick & dirty fix" and deny opening two tabs with the same page,or at least display a warning..

edit: @arjun: yes, i know, but i have to restore the search filters when the user return to the search page:) so $SESSION is the way to go for this. Also, the filters are sent down by AJAX and it gets hard to debug when you have lots of filters(GET is limited in size, so i use POST)

@tomhaigh: thx..but this is what i'm trying to do right now, but this will take a while because this tab "thing" affects the whole app, and i have to change the filters in all modules... i was looking for something quick&dirty for now. Tip: i dont want to use time() and rand() as theese functions can (and eventually will) generate duplicate numbers. In the new "rewrite" i use microtime from PHP and Date.getTime() in JS and concatenate theese 2 to generate a truly unique id. Also, i wrote a function to parse the SESSION from time to time and cleanup stored filters older than 2 hours so it wont grow too big.

@Gortok: i know..but is was designed with IE6 in mind and most of the users (something like 90%) still use IE6 while logging into this app... so i never saw the need to consider multiple open tabs.

A: 

Never deny a user from basic usability.

Ólafur Waage
A: 

Two issues I see:

  1. You're denying the user the ability to open the webpage in multiple tabs -- something that is nearly a requirement for web-applications.
  2. This issue brings up a design problem.

You may want to consider changing your $_SESSION variable to a singleton, and not allow it to be overwritten by multiple page views.

George Stocker
He already stated that he wants a quick and dirty temporary solution. Anyhow I can't understand how the singleton pattern helps in this case. Singleton $_Session? There is no application in php so there can't be a singleton either. Every object dies when response ends.
Diadistis
+1  A: 

Well this is a design problem that should be fixed asap. A quick and -surely- dirty way to deal with this though would be to store the client's IP address in $_SESSION ( $ip=$_SERVER['REMOTE_ADDR']; ) Then check and not create a new session or display a warning if multiple requests are made from this address. This would cause problems for visitors that are over a LAN that share the same IP address. I can't think anything more .. quick ( or dirty)

Laodimos
+6  A: 

I'd recommend avoiding the use of $_SESSION to store search filters, and instead encode them in the search query string (i.e. /search.php?filter1=val&filter2=val, etc). That way, each window has a unique URL and avoids clobbering another window.

arjun
+1  A: 

I agree with arjun's answer but if you must use sessions for this then you could create an array containing the information for each search inside the PHP session.

e.g. every time the form is submitted

$key = md5(rand(time(), 1));
if (!isset($_SESSION['search_filters'])) {
   $_SESSION['search_filters'] = array();
}

$filters = & $_SESSION['search_filters'][$key];

//store stuff
$filters = array( 'something' => true );

then you can construct a url like results.php?key=$key and then retrieve the data by

$filters = &$_SESSION['search_filters'][$_GET['key']];
if (!isset($filters)) die('cannot find search');

//do stuff with data
print_r($filters);

A problem you will have is that the session will get bigger and bigger as the user performs more searches, again I would go with arjun's answer, but I thought I would add another possible approach

Tom Haigh