SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    81
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Angry Arrays in Cookies

    I'm trying to store the contents of a shopping cart in a cookie... So I thought the best way would be to hold the product id and quantity in an array.

    I'm serialiZing the array when I store it but when I try to access it at a later stage it's converting to a boolean, hence I can't access the darn thing.

    Here's my code...

    $basket[$product_id] = $qty;
    $basket = serialize($basket);
    setcookie("basket",$basket,time()+604800);
    $basket = unserialize($basket);

    echo gettype($basket); -----> Shows "array"
    All seems to be well storing the cookie... When I'm trying to access the cookie with the following code...

    if(isset($_COOKIE[basket]))
    {
    $basket = unserialize($_COOKIE[basket]);

    if(array_key_exists($product_id,$basket))
    {
    ... more code
    }
    }
    I'm getting the error: Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in...

    If I run a

    echo gettype($basket); ------> Shows "boolean"??
    Anyone any ideas??? Even if I type cast the booloan to an array it still won't work?

    TIA

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What if you use:
    PHP Code:
    $_COOKIE['basket'
    not
    PHP Code:
    $_COOKIE[basket
    ?

    Why aren't you using sessions rather than cookies?
    Sessions are built for this kind of thing.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php.net/manual/en/function.unserialize.php
    "In case the passed string is not unserializeable, FALSE is returned and E_NOTICE is issued."
    So try to print $_COOKIE[basket] - I think it is may be corrupted and does not match original array.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    81
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    arkinstall: I want to store the contents rather than just hold them in a session... Then if the person logs off the site they can come back later and the cart will still be there.

    I've done a cart before where I stored the contents on the server using a session cookie to identify the person... Just thought I'd try holding the data on the client's pc rather than the server.

    I've tried your suggestion but it's still showing the same error. Thanks anyway...

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    arkinstall: I want to store the contents rather than just hold them in a session... Then if the person logs off the site they can come back later and the cart will still be there
    You store a cookie containing the session ID (can be done automatically by PHP), and store the data on the server. Storing them in cookies is like storing a CD in paper rather than it's case.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    81
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    $basket = unserialize($_COOKIE[basket]);

    I've tried

    echo $basket;
    print $basket;
    echo $basket[1];
    print $basket[1];

    '1' being the product_id

    Nothing shows up on screen...?

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    81
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    You store a cookie containing the session ID (can be done automatically by PHP), and store the data on the server.
    Yeah, I've done that in the past - I just don't want to store the data this time on the server, hence the (apparently corrupted) cookie array...

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ok, well try var_dump($_COOKIE) - that will tell you everything you need to know.

    Also, in your browser type this into the url bar:
    Code:
    javascript:alert(document.cookie);
    It will tell you your cookie info.

    As well as that, what it output if you simply echo the cookie rather than unserialising it?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    81
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Ok, well try var_dump($_COOKIE) - that will tell you everything you need to know.
    It produces: string(20) "a:1:{i:4;s:1:\"1\";}"

    i:4 --> is the product id
    \"1\" ---> is the qty

    ?

  10. #10
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If so, then var_dump(unserialize($_COOKIE)) should return you the array(4 => '1')
    array(1) {
    [4]=>
    string(1) "1"
    }

    It seems that $_COOKIE array was overwritten. Please try to var_dump($_COOKIE) at the very beginning of your script.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  11. #11
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You do realise that you don't have to serialize() arrays to put them in cookies, don't you?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  12. #12
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    81
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    You do realise that you don't have to serialize() arrays to put them in cookies, don't you?
    How else would you do it?

  13. #13
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $basket[$product_id] = $qty;
    setcookie($basket,time()+604800); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  14. #14
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    81
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    PHP Code:
    $basket[$product_id] = $qty;
    setcookie($basket,time()+604800); 
    Tried that before I looked @ the serialize stuff...

    Warning: setcookie() expects parameter 2 to be string, array given in.....

  15. #15
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah, yeah I don't use cookies in my applications (as you can probably tell):
    PHP Code:
    foreach($basket as $id=>$qty){
        
    setcookie("basket[{$id}]"$qty);

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  16. #16
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    81
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Finally figured it out

    I had to do type casts on the $product_id and $qty to make them both integers BEFORE loading them into the array. It was as "simple" as that.

    What I can't figure out is that why both variables weren't originally assigned as integers? but for some reason when I run them through a gettype they're being displayed as strings... totally bizarre.

    if(isset($_POST[qty])){$qty = $_POST[qty];}else{$qty = 1;}

    I'm not posting a qty over at the moment so it defaults to 1 - but as a string ??

    Anyway, finally got there. God knows how you would want to attempt to handle associative arrays through serializing cookies.

    Thanks for all your input guys.

  17. #17
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dablue View Post
    Anyway, finally got there. God knows how you would want to attempt to handle associative arrays through serializing cookies.

    Thanks for all your input guys.
    Late to the party, but I always use JSON to store cookie information. That way the Javascript can read it, too. There's functions that extend the JS string and array objects to allow you to read the encoded string and produce a JS object.


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
  •