Sessions vs. Cookies

Sessions vs. CookiesToday, I’ll be writing about when to use Sessions and when to use Cookies when developing your own  PHP Web Applications.

First off, I’ll talk a little about the pros and cons of Sessions.

  • Sessions are stored on the server
  • Clients won’t have any direct access to Session data
  • Accessing Session data is faster than retrieving a Cookie
  • Sessions can be  (theoretically) of unlimited size
  • Easier to use

Now on to Cookies.

  • Cookies can be set to last for a very long time
  • Cookie data is stored on the client computer
  • File size can be limited, but this depends on the client’s browser
  • Cookies are NOT dangerous. They really can’t do much of anything (harmful) without the help of some other malicious program.

Now that you know the basics of both Sessions and Cookies, you can now make an educated decision on which will work best in your project. To help, I’ll provide a few examples on what kind of projects you might use Sessions or Cookies on.

Sessions

  • Shopping carts/baskets
  • Anything that needs to hold sensitive information (passwords, etc.)
  • Where you don’t want the user to be able to directly see or edit the contents

Cookies

  • Login scripts that include a “Remember Me” function
  • Running on a cluster server environment. (If you have multiple servers that need to access the data)
  • If you need to have the data accessible for long periods of time

Happy developing! :D

This entry was posted on Thursday, March 19th, 2009 at 5:00 pm and is filed under Technology, Web Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

 

NO-WWW