Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

php sessions and cookies

  • 11-04-2006 11:55AM
    #1
    Registered Users, Registered Users 2 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