SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Old Fogey mancroft's Avatar
    Join Date
    Oct 2002
    Location
    lost
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Localhost session_id problem

    Localhost session_id problem

    Hello

    I am running PHP off localhost on my home computer and putting a shopping cart together.

    The code below sets the session_id OK but when I change from page1.php to page2.php, the session_id number is changed when it should stay the same.

    Is there a setting for session cookies in a PHP config file on the local machine that needs changing?

    Thanks.

    Code:
    function gettheid(){if (isset($_COOKIE["theshoppingcartdemoid"])){return $_COOKIE["theshoppingcartdemoid"];}else{session_start();
    setcookie("theshoppingcartdemoid", session_id(), time() + 3600 * 24 *30, "/", "",0);return session_id();}}
    :

    :

  2. #2
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you should use session_start(); at the beginning of every page, not just in a conditional. If you don't PHP will regive a new ID the next time it is called. Also, usually PHP sets the session ID itself (and sends the cookie), you don't need to do that manually. Also, you should keep your shop cart id in the session var $_SESSION instead.

    PHP Code:

    function gettheid() { 
      if ( isset( 
    $_SESSION['theshoppingcartdemoid'] ) )
        return 
    $_SESSION['theshoppingcartdemoid'];
      else
        return 
    NULL;
    }

    if ( !isset( 
    $_SESSION['theshoppingcartdemoid'] ) ) {
      
    // code to give a shop cart id here.

    David

  3. #3
    Old Fogey mancroft's Avatar
    Join Date
    Oct 2002
    Location
    lost
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BINGO! Thank you, David.
    :

    :


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
  •