-2

I am working on a shopping cart application. when user select particular product, then that product's id, quantity and price store in session. But when I close the browser all data from $_SESSION['cart'] is deleted, but I want when again I visit my site the last selected entity should be display. I am using price, qty and product id as -

<?
$_SESSION['cart'] = array();
$_SESSION['cart'][0]['productId'] = $pid;
$_SESSION['cart'][0]['qty'] = 1;
$_SESSION['cart'][0]['price'] = $price
?>

please tell me how to retain the selected values in session even after closing the browser. If you have any other approach? Thanks.

simon.denel
  • 780
  • 6
  • 23
nitin7805
  • 203
  • 2
  • 13

3 Answers3

1

Obviously, save the data to the cookies or database!
Session lasts until timeout (if there is any) or when you close the browser.

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
0

When you are setting the session data just set a cookie along with it:

setcookie('cart' , true, time()+60*60*24*7);
setcookie('productID' , $pid, time()+60*60*24*7);
setcookie('qty' , '1', time()+60*60*24*7);
setcookie('price' , $price, time()+60*60*24*7);

Than within a global header check to see if a person is not signed in (via session variable existing) and if they are not, check to see if the cookie exists, and if it is, populate your cart via that.

You will of course need to initiate a new session, but that is outside the scope of this question.

Abela
  • 1,225
  • 3
  • 19
  • 42
0

You can see session.cookie_lifetime in your php.ini. It is 0 by default. I think it is why your session expires when the browser is closed.

Set it to 86400 for setting session cookie lifetime to a day, and recheck your site. I think it will resolve your issue.

Have a look here

php.net article

Community
  • 1
  • 1
Vishnu R
  • 1,859
  • 3
  • 26
  • 45