Best way to retrieve a session id

Discussion in 'PHP' started by stuart, Jan 28, 2009.

  1. #1
    Is this the correct way to retrieve a session id for a new or returning visitor?

    
    if (!isset($_SESSION)){
    	session_start();
    }
    
    if(!isset($_REQUEST['PHPSESSID'])){
    	$log_session = session_id();
    }else{
    	$log_session = $_REQUEST['PHPSESSID'];
    }
    
    PHP:
    Thanks
     
    stuart, Jan 28, 2009 IP
  2. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The best way would be just to use $log_session = session_id();

    because user can actually modify PHPSESSID using a cookie editor.. so to be safe just user session_id()
     
    atlantaazfinest, Jan 28, 2009 IP
  3. InputProductions

    InputProductions Banned

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Assign a name value to the session, then call that session later in the script. Example:

    session_start();
    if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1;
    else $_SESSION['views']=1;
    echo “Pageviews:”. $_SESSION['views']
    Code (markup):
    Here's where I got the info on sessions: PHP Sessions
     
    InputProductions, Jan 28, 2009 IP
  4. stuart

    stuart Guest

    Messages:
    184
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for your help - I will go with session_id()
     
    stuart, Jan 28, 2009 IP
  5. qualityfirst

    qualityfirst Peon

    Messages:
    147
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Make sure you call session_start() regardless of starting a new session or continuing a previous one.
     
    qualityfirst, Jan 29, 2009 IP
  6. stuart

    stuart Guest

    Messages:
    184
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Great - Thanks!

    So this ...
    Is now...
    
    session_start();
    $log_session = session_id();
    
    PHP:
    And during testing the php notice "Notice: A session had already been started - ignoring session_start()" can just be suppressed.

    Thanks again for your help, that has cleared it up.
     
    stuart, Jan 29, 2009 IP