jammer Posted April 7, 2008 Share Posted April 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/99995-when-to-use-sessions/ Share on other sites More sharing options...
roopurt18 Posted April 7, 2008 Share Posted April 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/99995-when-to-use-sessions/#findComment-511344 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.