Forum Moderators: coopster

Message Too Old, No Replies

how to maintain a session like it would not conflict ion same browser

         

pyadav

4:57 pm on Jan 21, 2010 (gmt 0)

10+ Year Member



hi, i need a solution to main tain diss sessions in diff tabs in
same browser with same varibles of same application
looking for help
thanks

CyBerAliEn

8:07 pm on Jan 21, 2010 (gmt 0)

10+ Year Member



PHP doesn't know what window or tab you are viewing a site in. So there is an inherent problem here. You would need a way to tell PHP which tab/window a user is using.

The only solution that comes to my mind is doing some manipulation with Javascript/AJAX.

Essentially, you would have to use Javascript/AJAX to determine which window/tab a user is using to access the site. You can then use this information (from Javascript) to "let PHP know".

Method 1
With a regular "static"/PHP site, you'd probably have to run a Javascript call to see what tab is being used at that request. Then perhaps force a reload of the page with this info. EX: a request to '/home.php' might get bounced back as '/home.php?tab=123456'. You'd want to run the call first, before/while page loads so as to prevent the user from loading the page and then suddenly being redirected. Then, on your site, you'd need PHP code to check this 'tab' value. You can then determine which data to use given this info. You'd probably want to store the data/info into sessions in a format like:
_SESSIONS
>> [tabs]
>>>> [123456]
>>>>>> [all the data/info for this tab is under this array]
>>>> [9876]
>>>>>> [all data/info for this tab is under this array]

Then, given the tab the user is on, you will have different values/data to work with.

Note that this method is not efficient. Every single "first" request will result in ~generally: (1) 1st request made; (2) request accepted by server; (3) server process/parse request to PHP code; (4) process is outputted; (5) user gets output; (6) javascript results in page reload/new request with tab info/ID; (7) processed by server again; (8) final user output is sent; (9) user gets input

In other words, a single request to 'contact.php' will result in the server processing all the code and outputting it to the user TWICE. The first time to get the tab "ID" and then to return it via a new request, the second time to process the "returned" request /w tab info. So this will increase server loads and bandwidth usage. Something to consider!

And you cannot try to "save" this tab ID at any point via a URL (ie: contact.php?tab=345) or via session (ie: _SESSION['currentTab'] = "345";]. Why? Your whole premise is detecting the currently being used WINDOW or TAB. If a user were to visit a page, and a "current tab" marker created (and then subsequently used). If the user then reopens the site in a different tab or window, PHP will still think it is the same window/tab because it is using info from the PHP session or a URL (created from another tab/window). So you would have to make the window/tab check every single request.

Hopefully this gets you moving in the right direction. Implementing the above is not hard so much. It just has a decent share of concerns you have to consider. Besides server load and bandwidth, you have to consider whether the user would appreciate being redirected, how search engines might interpret the redirect (though unlikely given it is JS driven), etc.

Method 2
If your site is AJAX driven to the core, this would actually be very straight forward. In other words, if a request results in an HTML/PHP page being created that then calls on a JS/AJAX call to load the actual content/further pages.... this will be a lot easier simply because JS/PHP are already being intertwined.

All of the same ideas are the same as above. The key difference is this: In your master "HTML" file, you can have JS check for the "current tab", and then pass this info along with the AJAX request. Then, you can use this passed along "current tab" info with PHP to determine how to process/display content/data.

Hopefully this all makes some sense for you. :)

This also all hinges on Javascript being capable of telling different windows/tabs apart. I imagine it does, but I don't have any experience in having to deal with this.