getting started with PHP :: part 3

Using SESSION variables to determine the content that will be displayed.

To kick things off, you’ll need a basic form input on an index page. You would’ve created one in getting started with PHP :: part 2 which you can repeat if you fancy it; otherwise, just place this code into your html and save the page as ‘index.php’.

<p>here's a new form...</p>
<form action="home.php" method="post" enctype="multipart/form-data" name="myform" id="myform">
<label>Enter your name:
<input type="text" name="enteredname" id="enteredname" />
</label>
<input type="submit" name="submit" id="submit" value="Enter this name" />
</form>

It’s just a simple form with one textfield. Upon pressing submit, it’ll send the information to a page called ‘home.php’, which we’re about to create. So, make a new page and save it as ‘home.php’. At the very top of it (line 1 in the code, before any other html), add the following:

<? session_start(); ?>
<?
if ($_POST) {
$_SESSION['name'] = $_POST['enteredname'];
}
$name = $_SESSION['name'];
?>

Session start and variable creation

The first line initiates a session for the user which will begin when they come to this page in a browser. It’ll allow you to store information about that particular user for as long as they are on your site (or in fact, until they end the session, either via a link we will create later or by closing the browser).

We then have an if statement that checks if any form has be posted to this page and if so, creates a session variable called ‘name’ - assigning it a value of the ‘enteredname’ from the previous forms textfield. The reason it is inside an if statement is because we only want to set the value of ‘name’ once, when the form has be posted. Otherwise, the variable would get redefined (to *nothing* anytime the user loaded this page). Finally, for ease of use in the rest of our html, we create a local page specific variable called ‘name’ which has a value of the session variable ‘name’.

Now add a couple of links and a small bit of polite welcoming text within your html body further down the code.

<h1>Hello <?php echo $name; ?>!</h1>
<p>Click here to watch a video.</p>
<p>Click here to listen to some music.</p>

Now create another new page and save it as ‘video.php’. Add the session_start(); to the very top again (as above) and then on line 2, write:

<? $_SESSION['videopage'] += 1; ?>

Including ’session_start’ will enable you to use the variable previously created, but also to add new variables to the same session. This line does just that, creating one called ‘videopage’ and automatically adding a value of 1 to itself. This will be used to essentially record the amount of times the user comes to this page. On the first visit, the value will increase from nothing to 1, then by an additional 1 every subsequent visit. With this number now stored in the session, you can write some code to change the content depending on how many times the user has been on the page.

In your main body, add:

<? if ($_SESSION['videopage'] == 1) { ?>
<h1>Are you sure you want to watch a video?</h1>
<p>I don't think you do. Go back home.</p>
<? } else if ($_SESSION['videopage'] == 2) { ?>
<h1>You're quite persistant aren't you?</h1>
<p>But still, go home.</p>
<? } else if ($_SESSION['videopage'] >= 3) { ?>
<h1>Okay, fine. Watch this..</h1>
<p>(put a video here)</p>
<? }; ?>

This series of if statements will result in a different heading and sentence on the ‘video.php’ page each time the user visits it. After the third visit, you can let them watch a video. (Read the ’simple valid media embeds’ page COMING SOON :) to get a clean and easy way to do so).

Now to do a similar thing for the next page. Create a new page again and save it this time as ‘music.php’. Above all other code, write:

<? session_start(); ?>
<? $_SESSION['musicpage'] = 'visited'; ?>

This merely logs the fact that the user has been to this page by creating a variable and assigning a string value of ‘visited’. Then, using the code for an mp3 embed from the ’simple valid media embeds’ page, insert some music in the body and then add a link to the home page.

Music embed

Now, back on the home page - underneath the 2 links you currently have, add this:

<? if ($_SESSION['musicpage'] == 'visited') { ?>
<p>Click here to win millions of pounds!</p>
<? } ?>

This will generate a 3rd link on the home page, visible only after the user has gone to your music page. Clever innit(?) If you now create a page called ‘money.php’ and write a disparaging heading in the body with a link back home. Something like;

<h1>yeah right!</h1>
<p>go home</p>

Then, line 1, add:

<? session_start(); ?>
<?
unset($_SESSION['musicpage']);
$_SESSION['videopage'] = 0;
$_SESSION['greedy'] = true;
?>

We’re basically going to punish the user for being greedy and believing that you can make money from the internet. Ha! What a mental notion! The first line (unset) will remove the musicpage variable; then we’ll reset the videopage variable to 0 visits; and then we’ll log the fact that they were greedy.

Back on the home page, under the current heading you have, but before the 3 links, add:

<? if ($_SESSION['greedy'] == true) { ?>
<h2>You're very greedy. You might as well start again because I wont forget!</h2>
<? } ?>

The only way for the user to shake the ‘greedy’ tag is to restart the browser or to click the link provided to return them to the index page (the page that had the initial form on). To allow them to start afresh, now add these 2 lines to the top of your index page:

<? session_start(); ?>
<? session_destroy(); ?>

session_destroy does exactly what it sounds like it’d do. I don’t think I need to elaborate.

Presuming the user has gone through the entire process in their browser, they’ll now be back on the index page, looking at the form that’s there to acquire their name. However, there is one more thing to do. On the off chance that someone browses to the ‘home.php’ page without having started on ‘index.php’, they wont see the personalised welcome. We can’t have that can we(?) So finally, on ‘home.php’, in your php code underneath ‘$name = $_SESSION[’name’];’ add:

if (!$name) {
header("Location: index.php");
}

This will redirect to the index page if for whatever reason, the ‘name’ variable hasn’t been created. And that’s that.

For a working example of the pages created here, take a look at this.

Stuff.