Cookies can store only string values. It can't store array values. But we are try to add array values into cookies in PHP.
Where,
serialize() - Generate a storable values. Suppose you use above PHP code without serialize array value. Then you'll get error like this:
After serialize the array values, you can store array values into cookies using setcookie() in PHP.
Now the cookie contains array values.
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.
Now you can get output like this:
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);
?>
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:
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>';
}
?>
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:
$_COOKIES[''] is used to retrieve values from cookies.
Now you can get array values from cookies. The output is:
name : Guru
country : USA
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);
?>
$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>';
}
?>
$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:
Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
ReplyDeletewww.imarksweb.org
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
ReplyDeleteYour website is really cool and this is a great inspiring article. cbd vapes
ReplyDeleteI 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
ReplyDeleteThank 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
ReplyDeleteSuperbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.. just cannabis
ReplyDeleteI 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