Having different sessions with multiple browser instances can be done, but
it requires some less-than-simple effort. To have a different session you
must use a different session id.
Option (a) - you can bypass cookies and pass the session id within every
URL, but this presents a security risk as that session id is clearly visible
to the outside world and can be hijacked.
Option (b) - if you are using cookies (the preferred option) then the
session id is linked with a session name, the default being PHPSESSID. The
solution that I have found is to use a different session name for each
session. This allows the single cookie maintained by the web browser to
contain multiple session id's, each with their own session name.
Step 1 is to override PHP's default session name. I use a .htaccess file
with the following entry:
php_value session.name fred
Step 2 is to include a hidden field called "session_name" in every screen.
Step 3 is to execute the following code at the start of every script:
global $session_name;
if (isset($_REQUEST['session_name'])) {
// use session name passed via $_GET or $_POST
$session_name = $_REQUEST['session_name'];
} // if
Step 4 is to have the following code in your logon script:
// get details from any previous session
if (isset($session_name)) {
// use existing session name
} else {
// assign new session name
$session_name = getNewSession('menu');
} // if
session_name($session_name);
session_start();
session_unset();
initSession();
This uses the following user-defined functions:
function getNewSession ($prefix='fred')
// create a new session name using $prefix + a 1 digit number
{
// step through numbers 0-99
for ($i = 0; $i <= 99; $i++) {
$session_name = $prefix .$i;
if (!array_key_exists($session_name, $_COOKIE)) {
break;
} // if
} // if
return $session_name;
} // getNewSession
function initSession()
// standard session initialisation
{
....
if (!isset($_SESSION)) {
if (isset($session_name)) {
session_name($session_name); // set the session name
} // if
session_start(); // open/reopen session
} // if
....
} // initSession
Note that this will allow a suffix of 0-99 on the end of the session name of
"fred".
Step 5 is to have the following code at the start of every script
(immediately after the code identified in step 2):
initSession();
This has the following effect:
The URL for the logon screen does not contain the parameter "session_name",
therefore the logon screen will always generate a new session name.
The URL for every other screen will contain "session_name", therefore it
will continue to use the session with that name and the session id
associated with that name.
If within a browser window the user creates a copy of that browser window
then the existing session name will also be copied, in which case the same
session will be used by more than one browser instance. This can be remedied
by pressing the "logout" URL which will invoke the login screen which in
turn will generate a new session name and hence a new session id.
As you can see it is not trivial, but it can be done.
--
Tony Marston
http://www.tonymarston.net
"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in message
news:ab**************************@posting.google.c om...
ma******************@yahoo.com (john) wrote in message
news:<4c**************************@posting.google. com>... How do u guys handle multiple sessions??
i.e, opening different browser windows by running
iexplore.exe or clicking IE icons and opening the application. My
sessions are mixing up.
what i mean is
suppose i log in my site using username "test".
At this time I set $_SESSION['name']="test".
And I use $_SESSION['name'] inside my application to print the
username.
Now if I open another browser & log in with "another test" the session
variable is overwritten.Another session is not created.
Do you store the session in database or pass the session_id via get or
post.
IMO, this question has been asked so many times. You should have
searched the archives
<http://groups.google.com/groups?threadm=abc4d8b8.0312180003.4df5d9f5%40post ing.google.com>
--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com