Curricular Support Home / Resources / Tips and Guides / PHP Tutorial / Sessions in PHP

Using Sessions in PHP to Make Multi-Page Forms

If you want to make a form that spans multiple pages, you will have to use special functions. Normally, your page will lose all the data it has collected once a new page is opened.

Until the development of PHP, the most common way was to use cookies. A cookie is a small text file that stores information about the user on their home computer. Each time they revisit the site or move to a new page, the previously-saved information can be accessed by reading the cookie. This technique requires proficiency in JavaScript.

PHP can use hidden fields to send data back to the form using the echo() function, but this is a tedious programming project, and the coding process is very time-consuming.

Fortunately, PHP has some built-in functions that abstract all these complexities into a simple interface called sessions.

To use sessions in your form, insert the following start function into your code:

session_start();

Variables must be registered in the session to enable the features that a session provides. Use the following function to register a variable:

session_register("varname");

where varname is an HTML variable name; not a PHP variable name (i.e. the variable name does not begin with a $).

The session_start() and session_register() functions must be placed before the initial <HTML> tag in your web page's code in order for them to work properly.

If you no longer wish to use a variable in the session, you will have to unregister it using the following function:

session_unregister("varname");

To end a session and remove all instances of its use - including any registered variables - use the following function:

session_destroy();

Here is an example of sessioning in a multiage form: