Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

session_start() on every page? 1

Status
Not open for further replies.

Trusts

Programmer
Feb 23, 2005
268
US
Hi all,

I have an app with cart functionality. Some pages showcase items and from these pages you can add item(s) to the cart.
All of these "product pages" have the session_start() command at the top of the page.

Other pages are not product oriented, just informational - such as company history.

I just wish to confirm - get feedback from the group here that I have developed a good approach.

On the product pages, when a "add to cart" button is clicked I send the app to "update_cart.php" Users don't see this because it redirects back to the page they came from. But on the update_cart.php page I have this snippet:

session_start();
if(!isset($_SESSION['cart'])){
$ssql="Select Max(CartID) from tblCarts";
$result=mysql_query($ssql,$connection) or die ('unable to get maxcart');
$row=mysql_fetch_row($result);
$row[0]=$row[0]+1;
$_SESSION['cart']=$row[0];
}

So - at anytime the first item is added to the cart - that is when the session starts.

My questions are
1) is this a good approach - it forces a new cart to start upon the first item being selected, and
2) should I have the session_start() line on every page - such as the company history page? Or just keep it on the product pages.

I might even be mistaken that I need the session_start() on the product pages at all! Perhaps the update_cart.php is the only place where any session code is needed.

Thanks for any help!

- KB


 
i prefer to use sessions on every page so that you can use the session ID to track the user's time on your site. which information you can then use to improve your design as you go along.

for shopping carts i use a separate cart ID and store a mapping in the session keys and/or a database depending on the need for persistency.
 
Hi jpadie,

The shopping cart approach you have - sounds like mine - every item added to the cart, actually creates a database record with the cartID and the ItemID.

Just curious about the other use you mention. First, to do this I assume you need the "test if session exists yet" code snippet on every page? - because a user could come in one any of the pages? But then how do you track the time? Do you set a session of "time started"? and how do you know when the session ended?
Thanks,
KB
 
sorry - i wasn't being clear. i was using "time on your site" as a metaphor for their activity.

you can measure time, just record the click time. you would then also include a cronjob to close off inactive sessions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top