I need to set a cookie with an array as a value. Is the best way to run serialize() on the data?

Recommended Answers

I need to set a cookie with an array as a value. Is the best way to run serialize() on the data?

Yes. It's the best way to used serialize() function …

Jump to Post

All 2 Replies

Member Avatar

I need to set a cookie with an array as a value. Is the best way to run serialize() on the data?

Yes. It's the best way to used serialize() function on the data.

It should look something like this:

setCookie('admin', serialize(array(id => 'Feb2002', email => 'dani@dani.com')), time()+3600, COOKIEPATH);

or this example to see the differnece between serialized() & unserialize():

<html>
<body>
<pre>
<?php
  $array = array(
    'id'  => Feb 2002,
    'email' => 'dani@dani.com',
    'token' => base64_encode('LawnGuyland21593')
  );

  echo "Dump(initial):\r\n";
  var_dump($array);

  $serialized = serialize($array);
  echo "Serialized:\r\n".$serialized."\r\n";

  $unserialized = unserialize($serialized);
  echo "Unserialized:\r\n".$unserailized."\r\n";
  var_dump($unserialized);
  ?>
</pre>
</body>
</html>

or you can also used this:

http://snipplr.com/view/43435/

From that example from the link you need used json-decode function.

I'm not sure any of the examples I posted will work on codeigniter framework because codeigniter has it's own setcookies().

Hi,

Yes, that would be a good choice. The latest Symfony and Zend frameworks put everything into an array.. I don't have any symfony or Zend in front of me at the moment, but as far as I can remember while working with these two frameworks, both have sessions and cookies as an array.. it was something like this

## in symfony they have something like this

    public static function getSession($session_x = array(), $cookie_x = array(), $user_credentials=array()){

    ## something here

    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.