Multiple PHP sessions within the same browser instance

Dear all.....

I am currently writing a Help-Desk / Knowledge Base application using PHP/PostGreSQL.

I authenticate the user using a quite elaborate mechanism of cookies. The problem is that using cookies (I also have a version using sessions with the same problem), I can only seem to get one user logged in from any one browser instance. (Why would I want to be logging in as two users from one browser/IP? In case I want to open another browser tab, log in as "root" or whoever, and do some administration without logging out of my other session.)

For example, say I fire up Firefox, and log into my application. A session is started for the browser. I open another tab, and login as a different user, the session (and any session variables) are "overwritten" by this new session.

I have made a workaround for the time being, by writing IP addresses and usernames to a table, and only allowing one login from any one IP address. The problem here is that if the user forgets to logout, then session_destroy() (or manual cookie cleanup, depending on the version of my application) is never called, and when I roll this out the user will be calling the DBA/SA (me!) and I'd have to DELETE FROM ip_addr WHERE username = 'blah' AND ip_addr = 'xxx.xxx.xxx.xxx' to get rid of the entry and remove the lock - something I don't want to be doing for 150 users!!!!

In short, does anyone know a way to allow multiple independant sessions with independant session IDs from the *same* browser instance?

Any help would be greatly appreciated. If not, I can still stick to my one-login-per-IP method, as all our clients use different IP addresses and the proxy is bypassed on the local network.

is your database set up with a column called "privilages" or something (i.e. normal user or admin stored in here )

if so when the user logs in it saves the data to a cookie.... can't you just do an if statement doing something like
if user privilages are admin then store as a cookie called "adminCookie" else store as "userCookie", this might get around the problem, only if you are having 1 session as a user and another as an admin, but more than 1 session as a user or admin still wont work as it overwrites the cookie when you log in.

hope this helps, my brain is frazzled from work so don't know if i explained that well enough

good luck

Mark

I am fiddling around with variations of this now.... However, I have a set of columns in the users table that govern various permissions over the DB, (e.g. can create categories, can edit items, can delete items, etc, etc). So I can't really break it down into either admin or users, as I want to assign each user fine grained permissions - this is what makes setting the cookie (and then getting the page to render accordingly) quite difficult. As you say, if you login as somebody else the cookie gets clobbered and the "old" session assumes the values stored in the "new" sessions cookie.

It also becomes complicated when reading in cookie values. Say i'm logged in as both a user and an admin, and I try to remove an entry as the user, if the admin cookie exists as well, isn't it going to be a pain to decipher what's going on? (Because the script will be saying "if admin cookie exists - allow, else disallow), but both cookies will exist?

I think as a workaround for now, I will have to stick to limiting the thing to a single session per IP address. It's kludgy but will work. I think that it's just as easy to log out, and then log back in as a user with appropriate priveledges to do whatever needs doing. I've also set up a series of cookies that are set to various crypt()ed values to stop a user trying to forge a cookie with elevated priveledges and everything seems pretty secure at the moment.

Let me know if you come up with anything more (or if I've got the wrong end of the stick) - I'm still open to ideas and am still hacking around.

Thanks again,

Cheers
ZB

Did you try something like this?

Set cookie:

<?php

// include function files for this application
require_once('fns.php'); 
session_start();

//create short variable names
$username = $HTTP_POST_VARS['username'];
$passwd = $HTTP_POST_VARS['passwd'];

if ($username && $passwd)
// they have just tried logging in
{
    if (login($username, $passwd))
    {
      // if they are in the database register the user id
       if( $username == "Administrator"){
            $HTTP_SESSION_VARS['admin'] = $username;
            setcookie("cookie[$username]","Admin",time() + 10000000,'/','.website.com',0);
       }else{
            $HTTP_SESSION_VARS['valid_user'] = $username;
            setcookie("cookie[$username]",session_id(),time() + 10000000,'/','.website.com',0);
       }  
    }else
    {
      // unsuccessful login
      do_html_header('Problem:');
      echo 'You could not be logged in. 
            You must be logged in to view this page.';
      do_html_url('login.php', 'Login');
      do_html_footer();
      exit;
    }      
}
?>

Validate cookie

<?php
if(isset($_COOKIE['admin'])) {
  echo "<p>Hello Administrator</p><br />";
  echo $_COOKIE['admin'].'<br />';
  // after the page reloads, print them out
  if (isset($_COOKIE['cookie'])) {
    foreach ($_COOKIE['cookie'] as $name => $value) {
      echo "$name : $value <br />\n";
    }
  }
}elseif(isset($_COOKIE['valid_user']){
  echo "<p>Hello user</p><br />";
}

}else
  echo "<p>no cookie for you</p>";
?>

I would think for each admin task do an isset($_COOKIE['admin']).

I've got it sorted now, thanks.

I'm using session variables instead of cookies now, and call....

function check_admin_permission() {
   if ( $_SESSION[ 'p_comp' ] == 'Y' ) {
       return true;
   } else {
       return false;
   }
}

to check and then

$username = $_SESSION[ 'user' ];
if ( check_admin_permission() ) {
   print "<b>Welcome $username - You are an administrator</b><br />\n";
   print_admin_menu();
} else {
   // standard user
   print "<b>Welcome $username</b><br />\n";
   print_standard_menu();
}

and so on. I can then easily validate things on each page.

Cheers to everyone for their help.

Peace
ZB