SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cookie stored on computer and cookie code in MySQL database does not match

    Hi,

    I cannot work out why the cookie on my computer does not match the cookie id stored in the MySQL database.

    I have this function creating a cookie

    PHP Code:
    function GetCartId()
    {
    // This function will generate an encrypted string and
    // will set it as a cookie using set_cookie. This will
    // also be used as the cookieId field in the cart table

    if(isset($_COOKIE["cookieid"]))
    {
    return 
    $_COOKIE["cookieid"];
    }
    else
    {
    // There is no cookie set. We will set the cookie
    // and return the value of the users session ID

    session_start();
    setcookie("cookieid"session_id(), time() + ((3600 24) * 4));
    return 
    session_id();
    }

    And this code is used to enter the cookie ID into MySQL table (add an item to the basket):

    PHP Code:
    @mysql_query("insert into cart(cookieId, itemId, qty) values('" GetCartId() . "', $itemId$qty)"); 
    But then another query says this (get data about products added to basket only for user with particular cookie on their computer):

    PHP Code:
    $result mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" GetCartId() . "' order by items.itemName asc"); 
    This mysql query does not work. The problem is the "cart.cookieId='" .GetCartId() ."'". Without this it is fine. With it, it does not work.

    As you can see above the cookieId is defined by the GetCartId function. As far as I can tell it must match but with this bit of code it does not work.

    Please help,

    Matt.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2011
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Matt,

    I think after set_cookie, please echo $_COOKIE["cookieid"] to see if value of it match with one insert to DB.

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Within the MySQL I had set the cookie id to VarChar(20) so only the first 20 characters/numbers were being stored. My cookie is 32 characters/number on the computer. Is 32 the standard or should I cater for larger values?

    Matt.


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
  •