Jump to content

Keep Session Data Alive When Closing The Browser?


son
 Share

Recommended Posts

I have a shopping cart which stores all added/updated product data in sessions. How could I make the data permanent? I would like to work on a facility for user to press 'Store cart data' or similar, so when the user comes back to website the data is still available in cart (and can be manipulated in normal way). If the user does not press this key (or whatever the solution might be) the cart is empty as normal next time he/she accesses the page. What should I look into in order to achieve that?Son

Link to comment
Share on other sites

You could use cookies, or store the information to the database alongside the user's other information.
If I was to store data in database: How would user then retrieve the data into cart again, so he can add/delete products? The other solution with cookies: Is there a term I could search for? Would want to have a read about this.....Son
Link to comment
Share on other sites

The person would need an account, so after they log in you can look up their cart from the database. Cookies are just generic client-side storage, if you just search for how to use cookies there should be enough information there to cover the basics. The problem with cookies is that the user will only be able to use them if they're using the same browser on the same computer, if they go to another computer or use a different browser they won't have the cookies from the old one.

Link to comment
Share on other sites

The person would need an account, so after they log in you can look up their cart from the database. Cookies are just generic client-side storage, if you just search for how to use cookies there should be enough information there to cover the basics. The problem with cookies is that the user will only be able to use them if they're using the same browser on the same computer, if they go to another computer or use a different browser they won't have the cookies from the old one.
What would be the way to look up their data from database? First I would run a check to see if a session is created for the username (so he is logged in), then retrieve his user_id, then I check entries in the table that holds the saved product data for his user_id... How would I then get the data transfered into $_SESSION['cart']? Son
Link to comment
Share on other sites

You just save it in the session, I'm not sure what you don't understand about that. $_SESSION is just an array, so you make $_SESSION['cart'] and populate it with whatever data you want.
I tried several things, but does not work. I get it ok into user area and then display as:
$cartDetails_query = "SELECT * FROM cart_contents WHERE user_id = $user";$cartDetails_result = mysqli_query ($dbc, $cartDetails_query);if (mysqli_num_rows($cartDetails_result) > 0){echo "<table>\n";echo "<tr><th> </th><th>Product Code</th><th>Details</th><th>Quantity</th><th class=\"right\">Price</th></tr>\n";					while ($row = mysqli_fetch_array ($cartDetails_result, MYSQLI_ASSOC))					{					$product_id = $row['product_id'];					$_SESSION['cart'][$id] = $product_id;					$finish = $row['finish'];					$_SESSION['cart'][$id]['fn'] = $finish;					$ageing = $row['ageing'];					$_SESSION['cart'][$id]['age'] = $ageing;					$quantity = $row['quantity'];					$price = $row['price'];					$_SESSION['cart'][$id]['price'] = $price;etc

As you can see I have some attempts to put the data into session array for cart, but does not work... Where am I going wrong?Son

Link to comment
Share on other sites

$_SESSION['cart'][$id] = $product_id;$finish = $row['finish'];$_SESSION['cart'][$id]['fn'] = $finish;
In the first line, you create a member of $_SESSION['cart'] called $id. Does $id have a value? If not, then the script is either breaking at this point, or $id is being understood as 0. This may not be what you want.Then you assign a string/integer to $_SESSION['cart'][$id]. In the third line, you redefine $_SESSION['cart'][$id] as an array. So the string/integer value that originally belonged to $product_id is now lost.Maybe I'm wrong, but what I think you want to do is this:
$finish = $row['finish'];$_SESSION['cart'][$product_id]['fn'] = $finish;

Link to comment
Share on other sites

Another possibility. Your whole process, from start to finish, is this: You are starting with an array. Then you store all the values to a database. Later, when your user returns, you read all the values. Then you reassign them to an array. And when this is done, the array is an exact reconstruction of the original array.This could be a lot simpler if you serialized the original array as a string, saved the string, and then unserialized it when you want to reconstruct the array. The idea here is that instead of saving 10 or 20 values into multiple columns, you can save exactly one value in one column.Look at serialize() or json_encode().Your code could be as simple as this:$ready_for_database = base64_encode(serialize($_SESSION['cart']) );. . .$_SESSION['cart'] = unserialize(base64_decode($read_from_database) );Using base64_encode() allows the serialized string to be stored as a text data type. Longer but virtually mistake-proof.

Edited by Deirdre's Dad
Link to comment
Share on other sites

In the first line, you create a member of $_SESSION['cart'] called $id. Does $id have a value? If not, then the script is either breaking at this point, or $id is being understood as 0. This may not be what you want.Then you assign a string/integer to $_SESSION['cart'][$id]. In the third line, you redefine $_SESSION['cart'][$id] as an array. So the string/integer value that originally belonged to $product_id is now lost.Maybe I'm wrong, but what I think you want to do is this:
$finish = $row['finish'];$_SESSION['cart'][$product_id]['fn'] = $finish;

Hi Deirdre's dad,$_SESSION['cart'][$product_id]['fn'] = $finish;was what I was after. Thanks. It transfers the data ok into cart. Now the only issue is that in cart I cannot update quantities or delete the stored item. Would I need to delete data from database or what would be the best way to achieve that?Many thanks,Son
Link to comment
Share on other sites

You just need to make it update the database again based on the session data, whenever that is updated. You could even forgo sessions entirely and just query the database all the time.

Link to comment
Share on other sites

You just need to make it update the database again based on the session data, whenever that is updated. You could even forgo sessions entirely and just query the database all the time.
Would like to stay with Session data. Will try to delete once the data is moved and see if that brings me further...Cheers,Son:-)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...