PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: How to store array values into cookies in PHP

25 Jun 2014

How to store array values into cookies in PHP

                      Cookies can store only string values. It can't store array values. But we are try to add array values into cookies in PHP.

Store array into cookies in php using serialize():

                           
                    Consider the following PHP script:


<?php
error_reporting('E_ALL ^ E_NOTICE');
$val=array('name'=>'Guru','country'=>'USA');
$val1=serialize($val);
setcookie('values',$val1);
?>

             Where,
                serialize() - Generate a storable values. Suppose you use above PHP code without serialize array value. Then you'll get error like this:

Store array values into cookies in php

After serialize the array values, you can store array values into cookies using setcookie() in PHP.
Now the cookie contains array values.

Retrieve array values from cookies in PHP using unserialize():

                       
                     Consider the following example:

<?php
error_reporting('E_ALL ^ E_NOTICE');
$val=array('name'=>'Guru','country'=>'USA');
$val1=serialize($val);
setcookie('values',$val1);
$dat=$_COOKIE['values'];
$data=unserialize($dat);
foreach($data as $key => $vl)
{
   echo $key.' : '.$vl.'<br>';
}
?>

           Where,
                     unserialize() - takes serialized values and converts it into PHP value again. If you didn't use unserialize() in above PHP code, then you'll get error like this:

Retrieve array values from cookies in php

$_COOKIES[''] is used to retrieve values from cookies.
Now you can get array values from cookies. The output is:

                 name : Guru
                 country : USA


Store multiple values into cookies in PHP using json_encode():


                         You can also store multiple values into cookies using json_encode() method in PHP.
json_encode()  returns JSON representation of a value.  Consider the example:

<?php
$val=array('name'=>'Guru','country'=>'USA');
$ar=json_encode($val);
setcookie('cook',$ar);
?>

                      After encoded the array values, you can store it into cookies. If you use above code without json_encode(), then you'll get warning error message.

Retrieve array values from  cookies in PHP using json_decode();


                      You can retrieve array values from cookies using json_decode() like unserialize() in PHP.
json_decode()  takes JSON encoded values and convert it into PHP value again. Consider the example:

<?php
$val=array('name'=>'Guru','country'=>'USA');
$ar=json_encode($val);
setcookie('cook',$ar);
$return=$_COOKIE['cook'];
$arr=json_decode($return, true);
foreach($arr as $key1 => $values)
{
  echo $key1.' : '.$values.'<br>';
}
?>

Now you can get output like this:
        
                 name : Guru
                 country : USA

Related Post:

7 comments:

  1. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.

    www.imarksweb.org

    ReplyDelete
  2. Excellent website! I adore how it is easy on my eyes it is. I am questioning how I might be notified whenever a new post has been made. Looking for more new updates. Have a great day! cbd oil tinctures

    ReplyDelete
  3. Your website is really cool and this is a great inspiring article. cbd vapes

    ReplyDelete
  4. I think this is an informative post and it is very beneficial and knowledgeable. Therefore, I would like to thank you for the endeavors that you have made in writing this article. All the content is absolutely well-researched. Thanks... the cbd store

    ReplyDelete
  5. Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information. justcbd gummies

    ReplyDelete
  6. Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.. just cannabis

    ReplyDelete
  7. I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business. cbd edibles wholesale

    ReplyDelete