SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Brazil,Maringá-PR
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question how use n sessions at same time?

    How can I use two or more session with the same script execution?
    Here goes some study about session
    PHP Code:

    // even if I didn't set a value into $_SESSION,
    // the session cookie is created
    // session_write_close() is called automatic at the end of script
    // session_name is by default set to PHPSESSID
    // session_id is only auto generated after session_start,
    // even if you call session_regenerate_id before
    $sessionName session_name();
    session_regenerate_id();
    $sessionIdBefore session_id();
    session_start();
    // session_regenerate_id only works after session_start
    //session_regenerate_id();
    $sessionIdAfter session_id();
    echo 
    "session_name: $sessionName\n";
    echo 
    "session_id Before session_start: '$sessionIdBefore'\n";
    echo 
    "session_id After session_start: '$sessionIdAfter'\n"
    This is an example about using two sessions at the same time, but both are static, since the id is constant
    PHP Code:

    session_id
    ('id1');
    session_name('session1');
    session_start();
    $_SESSION['session1'] = 'session1';
    session_write_close();

    session_name('session2');
    session_id('id2');
    session_start();
    $_SESSION['session2'] = 'session2';
    session_write_close(); 
    Now only one session is static
    PHP Code:

    // default session
    session_start();
    $_SESSION['session1'] = 'session1';
    session_write_close();

    session_name('session2');
    session_id('id2');
    session_start();
    $_SESSION['session2'] = 'session2';
    session_write_close(); 
    The same as above, but with a workaround to use the default session after the custom session
    PHP Code:
    // save default session id and name
    session_start();
    $sessionId session_id();
    $sessionName session_name();
    session_write_close();

    // open session2
    session_name('session2');
    session_id('id2');
    session_start();
    $_SESSION['session2'] = 'session2';
    session_write_close();

    // restore default session
    session_id($sessionId);
    session_name($sessionName);
    session_start();
    $_SESSION['session1'] = 'session1';
    session_write_close(); 
    Are those methods good? There are technics betters?
    How can I have two or more sessions but none using static id?

  2. #2
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by neves
    Are those methods good?
    Why are you trying to do this? I am not saying it is a bad thing or anything, I am just curious to know. Perhaps then I can come up with an idea or two.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Brazil,Maringá-PR
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Marcus,

    This was only a curiousity exercise, too. But I came to it from the follow situation:

    I have a chat system that has user preferences stored on session. Nothing big until now. Now I want a global counter variabel that keep the number of users online. wrongly I stored the counter inside the session two, making the counter always be one to every body. I can easelly store this on a simple file, but this mistake make me thing about this session situation.

    Does it make you think, too?

  4. #4
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by neves
    This was only a curiousity exercise, too. But I came to it from the follow situation:
    In that case I don't think your example ties up with your original question. For complicated session management, limiting logins form the same IP for example, I would put the sessions into a fast DB like MySQL or Berkley. This means that global queries can be done, such as counting available sessions or IP checks, before authorisation is granted.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •