1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP array in a cookie?

Discussion in 'PHP' started by MuckyDucky, Sep 28, 2013.

  1. #1
    Good afternoon.

    I'm looking to store some data from some 'virtual' index cards. Each card has a front and a back, and the user can store multiple cards. Each side will have data on it.


    I ----------------- I
    I CARD 1 FRONT I
    I------------------I


    I --------------- I
    I CARD 1 BACK I
    I-----------------I

    I ----------------- I
    I CARD 2 FRONT I
    I------------------I

    I --------------- I
    I CARD 2 BACK I
    I-----------------I


    Imagine it from the diagrams above. I'd like to store each card's data (front and back) in a cookie, as an array (maybe), and then be able to pull each value back and insert it where applicable (on a different page). At the same time, bear in mind that the user can make as many cards as they like. I can't use POST or GET functions. The array bit is debatable, if you can think of an easier way of storing this data in a cookie, let me know. Please note: don't suggest storing in a database, as it won't be convenient for the project. :)

    I'm 14 years old, and PHP isn't my strongest language, so please keep it simple. :)

    If you need a better explanation, let me know. I've been thinking about this since last night but can't seem to think up a solution. :)

    Have a great day.

    Take care,
    MuckyDucky.
     
    MuckyDucky, Sep 28, 2013 IP
  2. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #2
    You can keep each of these card in 2 dimensional array like this:
    $card1= array("front"=>'card1_front', "back"=>"card1_back");
    $card2= array("front"=>'card2_front', "back"=>"card2_back");
    $card3= array("front"=>'card3_front', "back"=>"card3_back");
    PHP:
    Then save all these cards on an array itself.
    $cards = array($card1, $card2, $card3);
    PHP:
    Now serialize this array to save on the cookie:
    $serialized_cards = serialize($cards);
    PHP:
     
    samyak, Sep 28, 2013 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Why not just use a multidimensional array?
     
    $array = ('card1' => array('front' => 'card 1 front', 'back' => 'card 1 back'),' card2 ' => array(' front' => 'card 2 front', 'back' => 'card 2 back')) 
    $serialize_array = serialize($array);
    
    PHP:
     
    PoPSiCLe, Sep 28, 2013 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    I would suggest storing it in $_SESSION instead of a cookie -- really this sounds like data that has no business being passed client-side, particularly since as cookies it would be sent with EVERY file request both directions, slowing the page speed to abysmal levels.
     
    deathshadow, Sep 30, 2013 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Agreed. Cookies also has a finite number of bytes available (usually around 4000), and you might run out of space (depending on amount of data). Sessions does not really have a limit, so no need to consider that (unless you're going to have thousands of cards).

    $_SESSION will probably be a better solution.

    (Neither is a good solution, but if you need to approach the problem this way, the $_SESSION is better)
     
    PoPSiCLe, Sep 30, 2013 IP
  6. Ezeelive Technologies

    Ezeelive Technologies Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #6
    yes.. session is better solution than cookie..
     
    Ezeelive Technologies, Jan 2, 2014 IP