azunoman Posted January 8, 2009 Share Posted January 8, 2009 I use $_SESSION globals extensively. They work great until the user closes the browser. When they come back to our site their information, i.e. cart etc are long gone. I would like to generate a cookie that would be used to the session ID where I can then store $_SESSION (serialize) for subsequent retrieval. Can someone point me to an good tutorial? function or class....Thanks in advance for helping this old broke down 360 assembler programmer. Quote Link to comment https://forums.phpfreaks.com/topic/140049-saving-session-information-after-browser-closed/ Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 I use $_SESSION globals extensively. They work great until the user closes the browser. When they come back to our site their information, i.e. cart etc are long gone. I would like to generate a cookie that would be used to the session ID where I can then store $_SESSION (serialize) for subsequent retrieval. Can someone point me to an good tutorial? function or class....Thanks in advance for helping this old broke down 360 assembler programmer. I think you would be better off putting that info the DB instead of in a cookie. Either serialize the session array and put it in a DB column or make a column for each possible item in the array. I would make the DB field a text type and have the primary key be the userid, then on re-login check if they have a session in progress, if they do re-instantiate that session. EDIT: To do this you would want to update the DB each time the page is reloaded, so you have the most up to date session data stored. Quote Link to comment https://forums.phpfreaks.com/topic/140049-saving-session-information-after-browser-closed/#findComment-732756 Share on other sites More sharing options...
T-Bird Posted January 8, 2009 Share Posted January 8, 2009 The session id is stored in a cookie that is destroyed when the browser closes. You should be able to either edit the cookie expiration to some time in the future so as to make it remain after the browser closes, or just gather the session ID and post it in a new cookie. When the user returns read their cookie, and reset session name and id accordingly before starting the session. The rest of your session data can be stored in a database with PHPSESSID as the index. Don't forget though, if there is any sensitive data to require some form of validation so someone else can't edit their cookie to a random session id and highjack someone's data. Quote Link to comment https://forums.phpfreaks.com/topic/140049-saving-session-information-after-browser-closed/#findComment-732757 Share on other sites More sharing options...
azunoman Posted January 8, 2009 Author Share Posted January 8, 2009 "I think you would be better off putting that info the DB instead of in a cookie." I agree. I currently serialize session data and file it in a DB when someone adds something to a cart, as an example. "The rest of your session data can be stored in a database with PHPSESSID as the index." I would like to do this...again I already serialize info and store in a blob for later look see. Do I store the session ID on their machine by cookie so that when they return some time in < expiry time retrieve the session_id from the cookie then retrieve and unserialize $_SESSION into itself? Quote Link to comment https://forums.phpfreaks.com/topic/140049-saving-session-information-after-browser-closed/#findComment-732762 Share on other sites More sharing options...
PFMaBiSmAd Posted January 8, 2009 Share Posted January 8, 2009 For a shopping cart where you expect the data to not be lost if someone closes their browser, dog chews through their power cord, laptop battery dies..., you should be storing the cart items directly in the database instead of session. You will find that this results in the simplest code and for things like inventory management when someone does not complete an order, your cron job that updates the available quantity only needs to scan the pending orders past a certain date in the database, because all the necessary information will already be in the database instead of in leftover and possibly lost (due to garbage collection running) session data files. Quote Link to comment https://forums.phpfreaks.com/topic/140049-saving-session-information-after-browser-closed/#findComment-732803 Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 For a shopping cart where you expect the data to not be lost if someone closes their browser, dog chews through their power cord, laptop battery dies..., you should be storing the cart items directly in the database instead of session. You will find that this results in the simplest code and for things like inventory management when someone does not complete an order, your cron job that updates the available quantity only needs to scan the pending orders past a certain date in the database, because all the necessary information will already be in the database instead of in leftover and possibly lost (due to garbage collection running) session data files. Dont forget that some users, everytime their browser closes they wipe out their cookies (I do that all the time). Storing it in DB on each load is the best way to go imo. Then when that user logs in restore their shopping cart. If the user is not registered, well they should have registered. Quote Link to comment https://forums.phpfreaks.com/topic/140049-saving-session-information-after-browser-closed/#findComment-732806 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.