Both sessions and cookies are useful ways of storing data across web pages, and have their own advantages: sessions are more secure, and cookies can survive even after the user closes their browser.
You can rewrite the session system with PHP so that your session data is stored wherever you want - perfect for large, distributed server farms.
Unless you are using output buffering, cookies need to be sent before the main body of your page in order to comply with the HTTP protocol.
To store complex data types such as arrays and objects in a cookie or session you can serialize() to explicitly make them a string first.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Exercises >>
Previous chapter: Storing complex data types
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.