How to Store & Retrieve an Array Value in a Cookie

How to store and retrieve an array value in a cookie.  Cookies can only store string values. You cannot store an array directly into a cookie. In this article, We’ll discuss how to store and retrieve an array value from a cookie.

My previous post related to Cookies.

1. How to create, read, update and delete  a cookie in PHP

2. How to set a cookie on multiple subdomains


Subscribe Our Tutorials

Get Latest Updates on Facebook

How to Store an Array Value in a Cookie

For better understanding, let’s create a dummy array. Suppose we want to store this array value in a cookie.

Let’s try to set an array value into a cookie and see what happens.

If we try to set an array value then a cookie will not be created. To set an array value into a cookie first, we need to convert them into a string .

Now our cookie is set, We can retrieve their value using $_COOKIE global variable.

We can use a serialize method to convert an array into a string and then store them in a cookie.

Retrieving a  cookie value.

I find these two methods helpful for storing array values in a cookie. If you know any better method, you can discuss them in comments.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , . Bookmark the permalink.