SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict SwordsmanX's Avatar
    Join Date
    Sep 2005
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fastest way to check for session existence

    I'm trying to figure out the fastest (performance-wise) way to check if a session has not already been started
    Am I right to consider this the absolute fastest way?

    if (!isset($_SESSION))

    ?
    Any better suggestion?

  2. #2
    Non-Member
    Join Date
    Feb 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    //
    // Set page ID for session management
    //
    $userdata session_pagestart($user_ipPAGE_LOGIN);
    init_userprefs($userdata);
    //
    // End session management
    //

    // session id check
    if (!empty($HTTP_POST_VARS['sid']) || !empty($HTTP_GET_VARS['sid']))
    {
        
    $sid = (!empty($HTTP_POST_VARS['sid'])) ? $HTTP_POST_VARS['sid'] : $HTTP_GET_VARS['sid'];
    }
    else
    {
        
    $sid '';

    PHP Code:

            
    // session id check
            
    if ($sid == '' || $sid != $userdata['session_id'])
            {
                
    message_die(GENERAL_ERROR'Invalid_session');
            }

            if( 
    $userdata['session_logged_in'] )
            {
                
    session_end($userdata['session_id'], $userdata['user_id']);
            } 

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    NL, Rotterdam
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    PHP Code:
    if (!isset($_SESSION) && headers_sent()!=true)
    {
      
    session_start();

    Is what I use in objects that work with session variables. Hasn't failed on me yet...

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kendothpro View Post
    I'm trying to figure out the fastest (performance-wise) way to check if a session has not already been started
    You can't. php session functions don't allow you to check if a session exists without starting it. You can only do something like:

    PHP Code:
    session_start();
    if(isset(
    $_SESSION['started'])) {
       
    // continue with existing session
    } else {
       
    $_SESSION['started'] = 1;
       
    // use new session


  5. #5
    SitePoint Addict SwordsmanX's Avatar
    Join Date
    Sep 2005
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    You can't. php session functions don't allow you to check if a session exists without starting it. You can only do something like:

    PHP Code:
    session_start();
    if(isset(
    $_SESSION['started'])) {
       
    // continue with existing session
    } else {
       
    $_SESSION['started'] = 1;
       
    // use new session

    Mmmh I don't follow
    My understanding is that
    PHP Code:
    isset($_SESSION
    returns true if the session exists, and false if session_start() has NOT already been called

    Type-O's reply kind of confirms my belief that it's the fastest way (I don't use headers_sent since all my output is already held in a buffer with ob_start, but he makes a good point about headers)

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    If you don't start the session. ie session_start(); $_SESSION is not created.

    So just put session_start() at the top of the page and be done with it.

    And no if (isset($_SESSION)) if not faster then just calling session_start()
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Addict SwordsmanX's Avatar
    Join Date
    Sep 2005
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have an object with 5-6 functions, each of which will have to check if a value is already in the session, and if not calculate it, then write it to the session

    I can't blindly call "session_start()" in every single function, I'd end up with 10 stacked session_start, so I have to check for the session existence in advance!

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kendothpro View Post
    I have an object with 5-6 functions, each of which will have to check if a value is already in the session, and if not calculate it, then write it to the session

    I can't blindly call "session_start()" in every single function, I'd end up with 10 stacked session_start, so I have to check for the session existence in advance!
    PHP does the all by itself. What the heck are you doing any ways? Just put a single session_start on the page that calls those functions and do

    PHP Code:
    function something() {
        if (isset(
    $_SESSION['key']) 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Addict SwordsmanX's Avatar
    Join Date
    Sep 2005
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol nvm, I was messing with my "user" class and I didn't realize I was trying to persist the object in the session
    So I just removed all the pieces of "isset($_SESSION)" and just serialized the whole object in a session variable with the constructor and destructor, much cleaner


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
  •