jason360 Posted July 3, 2012 Share Posted July 3, 2012 Hey guys, I have a website that is using a login. I want add a shopping cart to the site in a separate folder (mysite.com/cart). I want the user to be able to say signed in for a couple weeks without having to re login so I am using: session_set_cookie_params(2*7*24*60*60, '/'); I am using sessions for my cart. The existing settings works fine with cart however the cart is holding the items for the length of the two week cookie params, and I want the cart to be reset when the window is closed, but still logged in. I have never used sessions before and logins. Is it possible to use different params or should i be using some kind of unset function? I am really not sure the best way to go about this. I don't want the user to have to login separately when they use the cart. Any ideas on the best way to do this would be appreciated. Please note i am a bit of a programming noob. Thanks, JK My cart script: mysite.com/cart/cart.php include("../config.inc.php"); session_name('wkLogin'); session_set_cookie_params(0); session_start(); if (isset($_GET['add'])) { $quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add']).''); while ($quantity_row = mysql_fetch_assoc($quantity)){ if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]) { $_SESSION['cart_'.(int)$_GET['add']]+='1'; } } header('location: http://www.mysite.com/cart/view.php?pid='.$_GET['add'].''); } function cart() { foreach($_SESSION as $name => $value) { if ($value>0) { if (substr($name, 0 , 5) =='cart_') { $id = substr($name, 5, (strlen($name) -5)); $get = mysql_query ('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id)); while ($get_row = mysql_fetch_assoc($get)) { $sub = $get_row['price']*$value; } } $total += $sub; } } if ($total==0) { echo "Your cart is empty."; } else { echo '<p>Total: $'.number_format($total, 2).'</p>'; } } My cart view item page: mysite.com/cart/view.php?pid=2 define('INCLUDE_CHECK',true); require('../config.inc.php'); require('../functions.php'); require('../cart/cart.php'); require '../login.php'; // initialization $result_array = array(); $counter = 0; $pid = ($_GET['pid']); // Full Size View of Photo if( $pid ) { $result = mysql_query( "SELECT * FROM products WHERE id='".addslashes($pid)."'" ); list($id, $name, $description, $snippet, $link, $price, $shipping, $quantity, $anchor) = mysql_fetch_array( $result ); $nr = mysql_num_rows( $result ); mysql_free_result( $result ); } ....... My login page: mysite.com/index.php define('INCLUDE_CHECK',true); require "connect.php"; require 'functions.php'; // Those two files can be included only if INCLUDE_CHECK is defined session_name('wkLogin'); // Starting the session session_set_cookie_params(2*7*24*60*60, '/'); // Making the cookie live for 2 weeks session_start(); require 'login.php'; Quote Link to comment https://forums.phpfreaks.com/topic/265137-session-params-and-shopping-cart/ Share on other sites More sharing options...
trq Posted July 3, 2012 Share Posted July 3, 2012 If you want to persist a login for that long use cookies. Sessions are not meant to outlive the current session. Quote Link to comment https://forums.phpfreaks.com/topic/265137-session-params-and-shopping-cart/#findComment-1358746 Share on other sites More sharing options...
sloth456 Posted July 3, 2012 Share Posted July 3, 2012 Another possible solution would be to store the contents of the cart in a table. When the user logs in again you can retrieve everything that was in his cart from the table. Quote Link to comment https://forums.phpfreaks.com/topic/265137-session-params-and-shopping-cart/#findComment-1358901 Share on other sites More sharing options...
jason360 Posted July 5, 2012 Author Share Posted July 5, 2012 Thanks guys...rebuilt my cookies and sessions. Quote Link to comment https://forums.phpfreaks.com/topic/265137-session-params-and-shopping-cart/#findComment-1359235 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.