Send PHP variables through multi page form

I am creating a small customer portal to capture invoice information.  I have created 4 pages for the user to walk through.
1. customer information
2. invoice information
3. desired sending method
4. confirm information and send

i would like for the data from each page to be carried throughout the system until the end.  I am currently pulling the information from page 1 to page 2 using a PHP variable and hidden form elements on page 2.  Then passing those hidden form elements to page 3 and so on.

Is that the best practice for passing the data from page to page or should i work some session variables?  If so, can someone give me some guidance on how to setup session variables and the capabilities of session variables.  thanks in advance.
Who is Participating?
morleysConnect With a Mentor Commented:
Storing this information in the session is the easiest way to manage data through a series of pages.
Early in your script (before you output anything) call: session_start();
You can then store and retrieve data using the the session array:
       $_SESSION['views'] = 1;
Generally you'll have a block at the start of the code which extracts data from the session into local variables with a usefull default
       $viewCnt = ISSET($_SESSION['views']) ? (int)$_SESSION['views'] : 0
If you have the same set of variables you will want to put that into a common subroutine.
On exit update the session with the new values
      $_SESSION['views'] = $viewCnt
axessJoshAuthor Commented:
I know just enough about PHP to be dangerous, but does the Session variables automatically get passed from page to page based on the session_start(); code?  What tells the second page that the session is still active?
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

but does the Session variables automatically get passed from page to page based on the session_start()  -->YES

hat tells the second page that the session is still active --> session_start()
An easier way for you to do this that would not rely on cookies which the user can have turned off is to simply setup hidden form variables on each new page.

For example lets say on customer info form you get name, address, phone then on invoice information form simply setup hidden fields with the values from form 1.  You can name them the same except just hidden and at the end when you confirm information and send it will simple send all the fields.

This works if they hit the back button as well but has the added benefit that if they have cookies turned off it will still work!
The session information is stored on the web server. A cookies is added to the response headers which provides the identity of the session and when returned from the client on the POST is used to reference the correct session data.

One advantage of session information is that you can store data that you don't want to send to and from the client so as to reduce the ability to hack the application.

Sessions do require that the client browser support cookies and its often a good idea to verify that in an early page of the site with a suitable warning that the site requires cookies.
axessJoshAuthor Commented:
so then that presents a new question,  which of the options would be the best and most effecient, session variables with its need for cookies to be enabled or hidden input fields variables?
Depends on your needs and the audience you are addressing. I have usually found that most people keep cookies turned on and the use of the session gives you lot of flexibility to store information securely. If you put the information into hidden fields you expose that data to your users and permit them to manipulate it. Alternatively the use of hidden fields allows you to support non-cookie users, does not require your users to respond in a fixed length of time (the session will generally have a small timeout period). You have to balance the various attributes of the methods against the needs of your application and the environment you will be operating in. Personally, I usually go for sessions unless there is some prevailing need not to.  
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.