Regarding the sessions in PHP(its very urgent please)

Discussion in 'Programming' started by mandarchalke29, Nov 28, 2007.

  1. #1
    Dear sir,

    I have created a chat application using PHP,Javascript and MySQL which is embedded in my web site. I have created a link to go from that website to chat application in the same browser. I have used a window.open to open a new window for chat application which will show you which users are online. Everytime when i click a user in the list a new window gets opened but the session used for both the windows is the same. I have taken that user's id from $user=$_GET['q]; where q is the userid. and stored the value of user in a session called $_SESSION[user_id]=$user; everytime when it opens a new window. The problem is that i need different session for different windows opened. Could anyone please help me. Its very urgent.

    Mandar Chalke:rolleyes:
     
    mandarchalke29, Nov 28, 2007 IP
  2. vonvhen

    vonvhen Peon

    Messages:
    152
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Dunno why you want to have different session...

    Anyway, I found this on the web...

    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
     
    vonvhen, Nov 28, 2007 IP