Helping ordinary people create extraordinary websites!

Go Back   Web Development Forum > Website Programming > Server-Side Scripting
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-24-2008, 10:32 PM
Junior Member
 
Join Date: Jan 2008
Posts: 24
Default Sessions vs Cookies with PHP

I would like to be able to maintain certain information about a user across the span of my site. Let say this is their username. I want this to be as easy as possible and somewhat foolproof. I am weighing the benefits and drawbacks of using cookies vs session info.

One question I have is, can sessions be used without displaying that awful session id info in the url?

Another is what are the pros and cons of using either of these methods?

Thanks in advance,
JL
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-30-2008, 06:21 PM
BigAlReturns's Avatar
Moderator Extraordinaire!
 
Join Date: Dec 2007
Location: The Wirral, England
Posts: 291
Send a message via MSN to BigAlReturns
Default

I would use sessions personally. They can be used without the id in the URL, in fact, I don't know how/why anyone does-it's certainly never been an issue for me! The cons of sessions would perhaps be a tiny increase in server load, but nothing I think would be of any concern. In terms of cookies, they are easily spoofed, and although sessions can be as well, it's a lot harder. I generally go with sessions for tracking per visit, and cookies to retain information between visits.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-24-2008, 06:33 AM
Junior Member
 
Join Date: Jan 2008
Posts: 10
Default

You should definitely go for sessions. Sessions are far more reliable, maintain state better and are under your control. Cookies, on the other hand, are often completely ignored by the browser or lost in window closes and opens etc. Leaving data in cookies also creates significant security issues and calls for all sorts of complex filtering to sanitize the data sent by the user, before you even get to constructing your page.

To disable PHP session IDs in the URL, add this to a .htaccess file:
Quote:
php_value session.use_only_cookies 1
php_value session.use_trans_sid 0
Most recent builds of PHP should have this setting set already; I believe it's been the default for quite some time. If your web host is on an old build of PHP (especially PHP 4), you might want to consider switching to a newer host.

Edit: See this article for more details.

Last edited by Draicone : 02-24-2008 at 06:35 AM. Reason: Add link to article
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 02:26 AM.


Website Design by Ducani Media Group
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.