Multi-page registration system: How to maintain state along the way?
I built a system to allow new members to join an organisation last year when I was just starting out as a programmer. It's a multi-step process across several pages that concludes after a successful credit card transaction. Part of the complication was the requirement for family memberships, which recorded the details of everyone in the family and created records for them as well.
Originally, I maintained the form input data across the steps using arrays in session variables and the data was committed to the DB after a the credit card payment went through.
I'm now completely rebuilding it since I've learned a lot since then and it's become too messy to maintain, but I still have no idea what the best way to manage this data is.
The original rationale was that because there were a lot of steps, and the requirement for a payment at the end, it seemed to make no sense to commit data to the DB until the process was complete, especially since it involved records in multiple tables and the idea of having to periodically flush out the incomplete records seemed like unnecessary and overly complicated effort. Having done it the first time around, the arrays in session variables got extremely messy an开发者_如何学Pythond involved a lot of verification in the code since it was potentially error-prone. I also lost a lot of time trying to correct for session timeouts (which seemed to happen far more often than I would have expected when I was developing).
So I ask those with far more knowledge and experience than myself: What is the best way to maintain temporary data through a multipage registration process?
There aren't that many different approaches to handle this generally speaking.
Either all the data will persist in the session, or just a unique identifier persists in the session that allows you to query out all the data that lives in the database throughout the process.
Sessions are volatile though, so the database is a better approach.
Save each step of partial data into the database, but have a column like "completed" or "committed" in each relevant table that is 0 by default, and only when the process is completed flag them all to 1.
It really isn't too much trouble to write a php script that gets called by cron once a night to go delete all the records that are committed=0 and last modified > 1 day (you do have a column that auto-updates a timestamp when modified right? ) or something like that. Or better yet instead of deleting, move them to another table called "warm_leads" - people who were interested in your service but bailed out for some reason and have sales/customer service follow up.
also, increase the session timeout to 1 or 2 hours:
ini_set(’session.gc_maxlifetime’, 120*60); // 120 minutes
Or have an AJAX request that fires every 10 minutes on each page that essentially is performing a session keep-alive to extend sessions as long as the browser is open and javascript isn't broken.
The other good news is with the database persisting data, you only need to validate it once on the way in, and not on every step.
Here is my other idea. If you are not wanting to use SESSIONS at all you could use Javascript (Yeah hear me out on this). So basically you would load the whole form at once then hide parts of it, when someone clicks next to move on to the next steps in the processes it would show (you could even animate a slide) the hidden part and hide what they just completed.
So you wouldn't be loading different pages but 'sliding' content in and out of view of the user. You could verify that the user has entered data as well before moving on, but you would not be saving the data and you wouldn't need to.
Here would be an example in jQuery (Note it doesn't show fields but you get the idea.)
http://cssglobe.com/lab/easyslider/03.html
Click the down button.
精彩评论