Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Array in cookie

  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array in cookie

    Hello All
    i have a problem in storing arrays in cookie i want to store or make cookie in which i can store multidimentional array........is it posible if yes then how i can do this?

    thanks in advance

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Have a look at serialize() and unserialize().

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    I'm also interested in this subject but didn't find the manual particularly eluminating on this occasion.

    Lets say I have a site with 5 pages, on each one users can vote on the page in order to say whether it's good or bad.

    This is an anonymous function so I can't use a database to store whether users have voted on each page or not, but I only want to allow each individual one vote (obviously cookies aren't full proof, but they're better than nothing).

    1) Is there some tutorial out there that would give me a practical example of how to use serialize() to store everything in one cookie, rather than dropping a cookie on every page?

    2) What if there were 10 pages. 20? 50? 1000? At what point does it become ridiculous trying to store all this in one cookie?

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,045
    Thanks
    10
    Thanked 93 Times in 91 Posts
    better off using sessions but the basics are the same
    PHP Code:
    <?php
    $data
    =array(
    array(
    'blah'=>'blah'),
    array(
    'blah'=>'blah')
    );
    setcookie('data',serialize($data));
    //..................another page
    print_r(unserialize($_COOKIE['data']));
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Pennimus View Post
    This is an anonymous function so I can't use a database to store whether users have voted on each page or not, but I only want to allow each individual one vote (obviously cookies aren't full proof, but they're better than nothing).
    If you can store it in a cookie, why can't you store it in a database? Are you setting cookies client side? Even then you can still store it in a database.

    Quote Originally Posted by Pennimus View Post
    2) What if there were 10 pages. 20? 50? 1000? At what point does it become ridiculous trying to store all this in one cookie?
    The cookies can easily be modified so its better to use sessions, like said.
    The cookies are sent in HTTP headers in each HTTP Response, and HTTP Request where you set a cookie. This can be up your bandwidth a lot (even worse for "Web2.0" Sites).
    The HTTP Specification recommends a limit on the amount of data that can be saved on the client via cookies.
    Lowest being IE6/7 which limits to 20 cookies per domain and total of 4KB (including temporary cookies such as sessionIDs).
    So you have to keep beneath this limit.
    Theoretically the best you would get space-wise would be to save in multiple cookies, since saving a serialized array includes the Array keys and information/syntax needed to unserialize the array .

    If you saved in a PHP session, however, all you have to worry about is the overhead when PHP reads/writes the session data to the local storage such as disk/db etc.
    If you're saving a small amount of data, like a preferred color, cookies are good. But if you already have a PHP session, then you already have the overhead of opening the connection to the local storage, writes/reads/updates are negligible load for the amount of data that usually goes to cookies.
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog

  • #6
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    If you can store it in a cookie, why can't you store it in a database?
    Based on what identifiable information about the user? It's an anoymous thing, i.e. anyone can do it, you don't have to be registered etc... perhaps I didn't make that clear.

    Re: Sessions. Don't those expire?

    PS: Thanks for the code Firepages.

  • #7
    New Coder
    Join Date
    May 2006
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Pennimus View Post
    Based on what identifiable information about the user? It's an anoymous thing, i.e. anyone can do it, you don't have to be registered etc... perhaps I didn't make that clear.

    Re: Sessions. Don't those expire?

    PS: Thanks for the code Firepages.
    Yes, the browser dumps temporary session cookies as soon as it is closed. (cookies that don't specify a expire date)

    I guess I was using the wrong term, "sessionID".
    What I meant instead of "sessionID" is saving an ID in a persistent cookie.
    (Just like PHP's built in sessionID, but with an expire date to make persistent).

    eg: based on what was posted already...

    PHP Code:
    <?php
    $data
    =array(
    array(
    'blah'=>'blah'),
    array(
    'blah'=>'blah')
    );
    $salt 'some really long secret string ....';
    $browserID sha1(rand(0100000).time().$salt); // your persistent sessionID
    setcookie('browserid'$browserIDtime()+3600'/'); // set the persistent cookie for 1 hour.

    // now insert the data into your db
    $database->query("INSERT INTO table set uid = '".$browserID."', data = '".serialize($data)."'"


    //..................another page

    // get data from db using id in cookie
    $data $database->query("SELECT data FROM table WHERE uid = '".$_COOKIE['browserid']."' LIMIT 1");
    print_r(unserialize($data);
    ?>
    this thus avoids the oversized http headers created if you have the cookies hold the data... and all other cookie problems...
    Fiji Web Design - where i do Joomla Web Design
    Bucabay.com - My blog

  • #8
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    Ahhh, cunning.... me likey


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •