Jump to content

When to use sessions


jammer

Recommended Posts

Hey i have a general question about sessions and when i should start the session.

 

Say i have a page that user can look at to add a product (add to shopping cart).  Should i start the session here?

 

or

 

should i wait and start the session on the next page where the user enters the quantity they want?

 

or

 

should i wait and start the session only after the user enters the quantity they want?  This is where i thought it should be but wouldn't it be easier to start the session from the very beginning?

Link to comment
Share on other sites

You use sessions to store user-specific data that is critical to your application and can't (or shouldn't) be stored in the URL.

 

For instance, you could design your application such that the URLs look like this:

http://www.mysite.com/shopping/<product_id>-<quantity>/<product_id>-<quantity>

 

Basically after the shopping portion of the URL you can store product_id-quantity combinations.  You could also use a get array:

http://www.mysite.com/shopping?product[id1]=quantity&product[id2]=quantity&product[id3]=quantity

In that example, id1, id2, id3, etc. are product IDs from your database.

 

To make your life easier, you should use mod_rewrite to funnel all http requests through a single index.php and you should start the session at the top of this script.  Sessions will always be active on your site, even when they're not being used, but it's very little overhead.

 

As for when you should use sessions, the best example is when a user logs in to a site.  You would typically store a flag in the session variable named 'User' that is null if the user is not logged in and their user_id if they are logged in.  That is usually all I store in session with the exception a small message that can be displayed after page redirects.

 

Other than that I use the URL for all of my parameters.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.