Reputation: 418
I am making a shopping cart and those people who haven't logged in, i am saving their choice of products in a session. Is their any way to expire a particular session on specific time like after 20 days or something?
Session["wishlist"] is the name of the session in which i am storing user's choice products.
Upvotes: 2
Views: 120
Reputation: 148180
If you have to keep the Session
for such long time period then you probably need to use cookies instead. If the user closes the browser then your session will get expired automatically and user wont keep the browser open for 24 days. Keeping session for long time could be burden without need on the server.
Response.Cookies["wishlist"].Value = "SomeValue";
Response.Cookies["wishlist"].Expires = DateTime.Now.AddDays(24);
Edit
You would probably store some unique identifier in cookie not the whole cart that will be used to get the shopping cart from the persistent storage like database. This will allow you to analyze the data later on for instance how many people get back and complete shopping etc.
Upvotes: 5
Reputation: 63772
Cookies are a good start in any case. Do note that you only want to do this for the simple data, eg. counts and item IDs. They can be modified by the user, so don't store price there :)
If you need to store more data, you might have to combine cookies with a database. Store an unique identifier in the cookie, and associate it with a row in the database. It is more complicated, since you need to handle the expiration manually, but it allows you to store a lot more information.
Upvotes: 0