Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php sessions and cookies

Options
  • 11-04-2006 11:55am
    #1
    Registered Users Posts: 5


    Hi

    I've written a script that creates a session when a user logs in to my site, where the session ends when they log out. That's working fine

    I've written another script, a simple shopping cart which creates a cookie saving a persons choices to the HD. That also works fine.


    The problem is that the cookie stores whatever choices are made on the computer... not neccessarily the choices made by the user who is logged in!

    In other words, how do I connect the two??

    login.php...

    // Start the session, register the values & redirect.
    $_SESSION = $row[1];
    $_SESSION = $row[0];



    db.php (an include in cart.php)...

    function GetCartId()
    {

    if(isset($_COOKIE["cartId"]))
    {
    return $_COOKIE["cartId"];
    }
    else
    {

    session_start();
    setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
    return session_id();
    }
    }


    cart.php...

    function AddItem($itemId)
    {

    global $dbServer, $dbUser, $dbPass, $dbName;

    // Get a connection to the database
    $cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

    // Checking if item already exists in cart table
    $result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId" );
    $row = mysql_fetch_row($result);
    $numRows = $row[0];

    if($numRows == 0)
    {
    // if item doesn't exist, add it

    @mysql_query("insert into cart(cookieId, itemId) values('" . GetCartId() . "', $itemId )");
    }

    }


    Any help and advice is greatly appreciated.
    edelmh:confused:


Advertisement