Hacking with PHP has been updated for PHP 7 - click here! >>


  • 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.


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

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.