ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

PHP and Cookies; a good mix!

(Page 4 out of 5)

One feature of cookies that is often missed in articles is the ability to story arrays. Cookies can be used to store multi-dimensional arrays, which can be extremely useful to store data.

Consider the following code;


setcookie ("name[first]", "Dennis", time() + (60*60*24));
setcookie ("name[last]", "Pallett", time() + (60*60*24));
?>

You can then display these two cookies using the following code:


echo "First Name: " . $_COOKIE['name']['first'];
echo "Last Name: " . $_COOKIE['name']['last'];
?>

The cookie 'name' is an array, and has multiple values. You can even go deeper and have multi-dimensional arrays, e.g. $_COOKIE['name']['test']['something']['value']. You could store whole arrays of data in cookies. But beware that you don't store too much data, there are certain size limits to cookies.

« Previous: Checking if cookies are enabled
Next: In Conclusion... »



One Response to “PHP and Cookies; a good mix!”

  1. Herojit Says:

    This site is very helpful to me.
    I expect more examples about session and cookies from this site.
    Thanks a lot.

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction
  2. Using Cookies
  3. Checking if cookies are enabled
  4. Storing Arrays
  5. In Conclusion...
Bookmark Article
Download Article
PDF
Download this article as a PDF file