Archive

Posts Tagged ‘session’

Multi Page Forms In PHP

May 5th, 2009 1 comment

Multi pages forms are just as they sound, a single form spread across multiple pages. These are useful in terms of usability as it can break up an otherwise dauntingly big form into smaller chunks. It can also be useful if you want to process some of the results in order to determine what forms the user sees on later steps.

There are two ways in which it is possible to do this using PHP.

The first (and simplest) is just to cycle through the items submitted on a previous form and print them out as hidden fields. Our first page source code will look like this:

<form action="form2.php" method="get">
Name: <input type="text" name="name" />
<br />
<input type="submit" value="Proceed">
</form>

On submitting the form we are taken to form2.php, which asks the user a different question and prints out the hidden fields. Because we used a get request for our first form we need to use the $_GET array.

<form action="end.php" method="get">
Colour: <input type="text" name="colour" />
<br />
<?php
foreach ( $_GET as $key=>$value ) {
    if ( $key!="submit" ) {
        $value = htmlentities(stripslashes(strip_tags($value)));
        echo "t<input type="hidden" name="$key" value="$value">\n";
    }
}
?>
<input type="submit" value="Proceed">
</form>

This same code can be used on the different pages of the form. This method is fine, and works quite well, but it doesn’t account for users going back through the form and resubmitting a previous item. The $_GET array will only contain information about the previous forms.

To make this more user friendly, and robust, we need to employ a session to store our form values as the user goes through the form. Using sessions means that the user can cycle back and forward through the forms with no ill effect on the form data. The following code will take the input of the previous form and save it as a PHP session.

session_start();
foreach ( $_GET as $key=>$value ) {
    if ( $key!="submit" ) {
        $value = htmlentities(stripslashes(strip_tags($value)));
        $_SESSION[$key] = $value;
    }
}

This must be included on every page of our multi page form. If it is not then the data will simply not be saved for that step. Your users can now move back and forward through the forms, saving the information as they go. You need to supply a back button for this to work as using the browser back and forward buttons will also not save the data.

Finally, when testing this code I found that using GET rather than POST was beneficial in terms of usability. This is mainly because if you use POST requests and the user clicks the back on their browser they will be asked if they want to resubmit the information for that form.

Does anyone else have any ideas about how to do this? If so then post a comment and suggest it. You can even put a post in the forum!

Categories: PHP Tags: , , , , , , ,

Remembering Authenticated Sessions With Zend Framework

January 27th, 2009 No comments

After setting up your session management in your application using one of the Zend_Auth adapters you might want to allow users to stay logged in. What you need to do is set some configuration options in the Zend_Session object. Zend_Auth uses Zend_Session as an object orientated way of manipulating the $_SESSION variable. Any changes you make to the Zend_Session object will affect the Zend_Auth object, as long as you set these options before the sessions are started.

There are a number of configuration options available, but for the effect I was looking for I only needed to change the ones below. You might not need to set all of these, but it gave me the best cross browser behavior.

[live]
sessions.name = SESS_NAME
sessions.strict = off
sessions.use_only_cookies = on
sessions.cookie_lifetime = 12345678
sessions.remember_me_seconds = 12345678
sessions.gc_maxlifetime = 12345678

12345678 is the number of seconds, which is about 6 months.

You can load these configuration options into your session using the following code.

// load the config file
$configuration = new Zend_Config_Ini('config.ini', 'live');
// load the config file into the session options.
Zend_Session::setOptions($configuration->sessions->toArray());

Put this in your bootstrap file so that it is loaded before your Zend_Auth call. You should now find that your users are able to close down the browser and reopen it with the session intact.

Using PHP Sessions To Detect Returning Users

February 3rd, 2008 2 comments

To detect a user returning to a web page you can use the built in PHP session manager. At the start of the code you can use the session_start() function to initiate the session, you can then use the $_SESSION global array to store and retrieve information. The session_start() function sends a cookie to the client with a unique code that looks like this

8a9af5644326881594811db6fe96faf8

The session variable information is kept in a file on the web server and when the session_start() function is called PHP looks for the file with the corresponding name. It then parses this file and loads the variables into memory.

<?php
  session_start();
 
  if(isset($_SESSION["last_visit"])){
    echo "Date of last visit: ";
    echo date("j F Y, H:i:s",$_SESSION["last_visit"]);
    echo "<br />";
    echo "Total visits: ".$_SESSION["num_visits"];
    $_SESSION["num_visits"]++;
  }else{
    echo "This is your first visit";
    $_SESSION["num_visits"]=1;
  }
 
  $_SESSION["last_visit"] = time();
?>

The two pieces of information stored here are last_visit and num_visits. The code checks to see if the last_visit variable is set, if it is then the user has visited before so the code can increment the num_visits variable. If last_visit isn’t set then the user has not visited before and so

The last_visit variable is always set at the end of the code.

Categories: PHP Tags: , , , ,