Hudzilla.org - the homepage of Paul Hudson
Contents > Cookies and Sessions Wish List | Report Bug | About Me ]

10.5     Summary

This is NOT the latest copy of this book; click here for the latest version.

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





<< 10.4 Storing complex data types   10.6 Exercises >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 20 Aug 2008

I wouldn't suggest putting the username in a cookie, and ONLY looking for the cookie, because they could always change the cookie to something else if you don't make sure that they're who they say they are!

singpolyma AT gmail.com - 20 Aug 2008

One thing I would like to note is that there is a way to have the permanece of cookies with the security of sessions. What you do is you store a unique ID (or if you have a login, a username) in a long-lasting cookie youself. Then you store their data in a database and reference it manually using the cookie. This is what I use for user account data on my websites and it works very well.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is two plus six?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow