Alternative Home ButtonCIT 235 logo
School
  • CIT 230
  • CIT 235
Bio
  • Mike and Brit's Blog
  • Our Family
Current Projects
  • Hoggan's Rock Solid Gear
    Test Site
  • PHP Motors
Portfolio
  • BurgWear
  • Nature's Nook
  • The Auto Spa Plus

You Are Here: Home > School's Main Page > CIT 235 Main Page > WDS Main Page> WDS 6

WDS 6

Cookies and Sessions

Now what can I tell you about Cookies and Sessions? Well the great thing is it is not too complicated to understand how they work and how they actually can be a benefit, not only to the web developer but more importantly the client and user.

If you are unfamiliar with what a Cookie is, it is PHP code that preserves some form of state of a webpage or application. Think of it this way; you have logged into to your favorite site and you pay attention to a little welcome blurb at the top of the page by the header and navigation and it says, "Welcome back, Dave." - by the way, you are Dave - Now this makes you wonder, "How the heck did muffler.com know that I left and had returned?" Cookies my friend. Muffler.com has some PHP code embedded, the cookie and as you work on the page, your browser requests a URL from the web server that corresponds to the PHP code. The server then sends the information back to your browser and stores it right there on your computer. Did I lose you? To be less confussing, here is a diagram that helps you see how the cookie makes it to your browser and how it works.

The life cycle of a cookie

The life cycle of a PHP-generate cookie

  1. As I said above, the browser requests a URL from the web server that corresponds to the PHP code
  2. The server then sends the information back to your browser and stores it there right on your computer
  3. When your browser recieves this HTTP header, the browser creates and stores the specified value as a cookie named mycookie
  4. Now any request you make from your browser contains a HTTP header that sends name and value of the cookie
  5. Now PHP will automaticlly create can array with the name of the cookie and its value

I hope this helps you understand a little bit of what is going on with a cookie.

Sessions

Now Sessions are pretty much the same idea as a cookie. A difference lies in where your information is stored. With a cookie, information is stored on your browser on your computer. Sessions stores the information on the web server. The reason each were created were due to multiple reasons. One is that some users have cookies disabled in thier computer, which makes shopping and storing items in a shopping cart very difficult if you have to leave and come back later. Another reason is instead of storing lots of information, maybe even sensitive information, sessions will store only cookie that contains a sessions ID on your browser so that PHP can use it to load the stored data on thier server that is associated with that session. Cool eh? So I hope this helps you understand the reasons and methods of how cookies and sessions work.

Back

Last Updated on April 4, 2009 3:57 PM

© 2008, Mike Hoggan, All rights reserved
Contact Me | Terms | Privacy | Colophon | CIT 230 | CIT 235 | Mike and Brit's Blog
Our Family | Hoggan's Rugged Gear Test Site | BurgWear | Nature's Nook | The Auto Spa Plus

Valid CSS! Valid XHTML 1.0 Strict