I had written this tutorial sometime ago with the intention of cleaning it up and posting it here. Finally getting around to this. The original purpose of the tutorial was to explain how to use session cookies to a friend of mine who was working on a form that was a number of pages long. I apologize if it seems like I'm starting in the middle of an explanation (I am a bit). The tutorial assumes you've created a few form pages that are connected to each other and that the end result will be taking the data from all the pages and dumping it into a database. This tutorial really focuses on just the session cookie piece and does not go into how to create the forms, or do the inserts for the database.
Since the form spans across a number of pages, the best way to handle this "in between" stage is to store all the values in a session cookie. The reason being, that $_POST array disappears after the first page. Just using the $_POST array, you could spit back all the information to the page – however, in order to submit the data again to be processed and inserted into a database, you'd need to put it into yet another form – specifically hidden values. You can do it this way – there's nothing wrong with it really – it's just more of a pain. By putting all the values into a session cookie, if the user clicked the "back" button to make changes, their info is still remembered, where as with $_POST – it wouldn't be there any more and they'd have to start from scratch! (I know that firefox tends to remember the data – but IE won't)
side note: difference between a session cookie and a regular cookie – session cookies only last as long as your browser is up and running. They will "timeout" after a while too – how long before it times out is a setting in PHP itself (that php.ini file) on the server. Usually it's a few minutes. If it's not long enough – then session cookies probably aren't appropriate for what you need to do! Regular cookies are stored on the users machine. You can set the "lifespan" of this cookie to be several minutes or even several years. So obviously it doesn't go away if the user closes their browser, etc. Since we're only using this information for a moment, we probably wouldn't want the browser to remember it forever and ever – which is why session cookies are perfect.
Ok, so here's how you put all the post values into a session cookie.
First thing – you need to tell the page you're using sessions. Start the session by putting this snippet of code above ANY html tags (including doctype declarations)<?php
session_start();
?>
In this next step we'll want to iterate through that $_POST array and store each piece of information in a similar session cookie array.
To create the session cookies – very easy:<?php
foreach($_POST as $k=>$v) {
$_SESSION['post'][$k]=$v;
}
?>
(I've actually mentioned this method before)
What that says is – for each piece of information we have in the $_POST array – $k will be the index name and $v will be the value – So if you have a field with the name "First_Name" it's value is now stored in this session cookie: $_SESSION['post']['First_Name'].
Another side note – the reason why I'm storing them in a sub-array $_SESSION['post']['FIELDNAME'] as opposed to just $_SESSION['FIELDNAME'] is in the event that the field name overlaps with something else that might be used. In fact, you can rename 'post' to be specific to the form you're working on to ensure it's not overlapping with anything else… ie. $_SESSION['myContactForm']['FIELDNAME']
Ok – once you do all the above (start the session – get all the data into a session cookie, etc.) you can then display each piece by using the session cookies. For example if you want to do a "confirm" page that spits back all the data that was entered before it gets submitted. You just echo all the session cookies like this: echo $_SESSION['post']['First_name']
On this "confirm" page – you can have a simple form with just a "confirm" and "go back" button. If you want people to be able to go back and edit their entries, you can make the fields in the form pages be "pre-populated" using the session cookie values like this:
First Name: <input type="text" name="First_Name" value="<?= $_SESSION['post']['First_Name']; ?>">
Unfortunately – checkboxes and dropdowns need to be handled differently. Because we're not displaying the value – we need to check each option – and if that's what had been selected, then we echo the text selected="selected"
To do the check you could do it "long hand" like this:<option value="NEW YORK" <?php if ($_SESSSION['post']['state'] == "NEW YORK") {
echo 'selected="selected"';
} ?> >NEW YORK</option>
and do that for each item – or you can do it the "shorthand" way – which I prefer for this much repetition – makes the code cleaner too:<option value="NEW YORK" <?php echo $_SESSION['post']['state']=='NEW YORK'?'selected="selected"':''; ?> >NEW YORK</option>
Another side note: the way that shorthand works is this:
CONDITIONAL_STATEMENT ? Do_this_if_true : Do_this_if_false
Once we're done updating the database, we'll want to clear out our session cookie:
<?php
unset($_SESSION['post']);
?>