O'Reilly logo

PHP and MySQL for Dynamic Web Sites: Visual Quickpro Guide, Second Edition by Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sessions and Cookies

In the previous examples I’ve accomplished the same tasks (logging in and logging out) using cookies and sessions. Obviously, both are easy to use in PHP, but the true question is when to use one or the other.

Sessions have the following advantages over cookies:

  • They are generally more secure (because the data is being retained on the server).

  • They allow for more data to be stored.

  • They can be used without cookies.

Whereas cookies have the following advantages over sessions:

  • They are easier to program.

  • They require less of the server.

In general, to store and retrieve just a couple of small pieces of information, use cookies. For most of your Web applications, though, you’ll use sessions. But since sessions do rely upon cookies ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required