Moving beyond the simple access counter, you can use sessions to manage your users' preferences when they visit your site. In this three-step example, you'll start a session, ask a user for her font family and base font size preferences, display those preferences on subsequent pages, and allow the user to change her mind and reset the values.
In this script, you'll start a session and register the font_family and font_size variables. The displayed HTML will be a form that allows you to change your preferences.
Open a new file in your text editor, start a PHP block, and call the session_start() function:
Start an [if...else] block to check for any previous values for font_family and font_size. If values are not present in the current session, assign default values and add them:
if ((!$_SESSION[font_family]) || (!$_SESSION[font_size])) {
$font_family = "sans-serif";
$font_size = "10";
$_SESSION[font_family] = $font_family;
$_SESSION[font_size] = $font_size;
If previous values do exist, extract the values from the $_SESSION superglobal:
} else {
$font_family = $_SESSION[font_family];
$font_size = $_SESSION[font_size];
}
| Note |
Because the user will come back to this script to reset her display preferences, you have to take into account the fact that the values of the variables must always be extracted from the session itself. If you simply added the variables to a session without checking for previous values, each time the page was loaded, the value of these variables would be overwritten as an empty string. |
Close the PHP block and type the following HTML:
?> <HTML> <HEAD> <TITLE>My Display Preferences</TITLE>
Create a style sheet block, starting with the opening <STYLE> tag:
<STYLE type="text/css">
Add a style sheet entry for the BODY, P, and A tags. Mingle HTML and PHP to display the current values of $font_family and $font_size:
Add a style sheet entry for the H1 tag. Mingle HTML and PHP to display the value of $font_family and a modified value of $font_size (base value plus 4):
H1 {font-family:<? echo "$font_family"; ?>;
font-size:<? echo $font_size + 4; ?>pt;font-weight:bold;}
Close the </STYLE> tag and continue with the HTML, adding a heading and beginning a form. Assume that the form method is POST and the action is session02.php:
</STYLE> </HEAD> <BODY> <H1>Set Your Display Preferences</H1> <FORM METHOD="POST" ACTION="session02.php">
Create a set of radio buttons from which the user can choose a new font family:
<P>Pick a Font Family:<br>
<input type="radio" name="sel_font_family" value="serif"> serif
<input type="radio" name="sel_font_family" value="sans-serif"
checked> sans-serif
<input type="radio" name="sel_font_family" value="Courier"> Courier
<input type="radio" name="sel_font_family" value="Wingdings"> Wingdings
</p>
Create a set of radio buttons from which the user can choose a new base font size:
<P>Pick a Base Font Size:<br> <input type="radio" name="sel_font_size" value="8"> 8pt <input type="radio" name="sel_font_size" value="10" checked> 10pt <input type="radio" name="sel_font_size" value="12"> 12pt <input type="radio" name="sel_font_size" value="14"> 14pt </p>
Add a submit button and close the form:
Add some more HTML so that the document is valid:
</BODY> </HTML>
Save the file with the name session01.php, and place this file in the document root of your web server.
Your entire code should look like this:
<?
//start a session
session_start();
//check for stored values and register defaults
if ((!$_SESSION[font_family]) || (!$_SESSION[font_size])) {
$font_family = "sans-serif";
$font_size = "10";
$_SESSION[font_family] = $font_family;
$_SESSION[font_size] = $font_size;
} else {
//extract from $_SESSION superglobal if exist
$font_family = $_SESSION[font_family];
$font_size = $_SESSION[font_size];
}
?>
<HTML>
<HEAD>
<TITLE>My Display Preferences</TITLE>
<STYLE type="text/css">
BODY, P, A {font-family:<? echo "$font_family"; ?>;
font-size:<? echo "$font_size"; ?>pt;font-weight:normal;}
H1 {font-family:<? echo "$font_family"; ?>;
font-size:<? echo $font_size + 4; ?>pt;font-weight:bold;}
</STYLE>
</HEAD>
<BODY>
<H1>Set Your Display Preferences</H1>
<FORM METHOD="POST" ACTION="session02.php">
<P>Pick a Font Family:<br>
<input type="radio" name="sel_font_family" value="serif"> serif
<input type="radio" name="sel_font_family" value="sans-serif"
checked> sans-serif
<input type="radio" name="sel_font_family" value="Courier"> Courier
<input type="radio" name="sel_font_family" value="Wingdings"> Wingdings
</p>
<P>Pick a Base Font Size:<br>
<input type="radio" name="sel_font_size" value="8"> 8pt
<input type="radio" name="sel_font_size" value="10" checked> 10pt
<input type="radio" name="sel_font_size" value="12"> 12pt
<input type="radio" name="sel_font_size" value="14"> 14pt
</p>
<P><input type="submit" name="submit" value="Set Display Preferences"></p>
</FORM>
</BODY>
</HTML>
Now open your web browser and type http://127.0.0.1/session01.php.
Unless you closed your web browser between the last script and now, your old session will still be active and you won't see the cookie approval dialog box. You should just see the following figure.
In the next section, you create the script that handles the preference changes.
In this script, you assign the new values for font_family and font_size, and display a confirmation that the changes have been made.
Open a new file in your text editor, start a PHP block, and call the session_start() function:
<? session_start();
Start an if…else block to check for the posted valued for font_family and font_size. If values are present, add them in the session:
if (($_POST[sel_font_family]) && ($_POST[sel_font_size])) {
$font_family = $_POST[sel_font_family];
$font_size = $_POST[sel_font_size];
$_SESSION[font_family] = $font_family;
$_SESSION[font_size] = $font_size;
Continue the block to check for previously stored values for font_family and font_size, but only if the posted values are not present:
} else if (((!$_POST[sel_font_family]) && (!$_POST[sel_font_size]))
&& ($_SESSION[font_family]) && ($_SESSION[font_size])) {
$font_family = $_SESSION[font_family];
$font_size = $_SESSION[font_size];
$_SESSION[font_family] = $font_family;
$_SESSION[font_size] = $font_size;
Finally, if values are not present from the form or from a previous session, define and add some defaults:
Close the PHP block and type the following HTML:
?> <HTML> <HEAD> <TITLE>My Display Preferences</TITLE>
Create a style sheet block, starting with the opening <STYLE> tag:
<STYLE type="text/css">
Add a style sheet entry for the BODY, P, and A tags. Mingle HTML and PHP to display the current value of $font_family and $font_size:
BODY, P, A {font-family:<? echo "$font_family"; ?>;
font-size:<? echo "$font_size"; ?>pt;font-weight:normal;}
Add a style sheet entry for the H1 tag. Mingle HTML and PHP to display the value of $font_family and a modified value of $font_size (base value plus 4):
H1 {font-family:<? echo "$font_family"; ?>;
font-size:<? echo $font_size + 4; ?>pt;font-weight:bold;}
Close the </STYLE> tag and continue with the HTML, displaying the values of the two registered session variables:
</STYLE> </HEAD> <BODY> <H1>Your Preferences Have Been Set</H1> <P>As you can see, your selected font family is now <? echo "$font_family"; ?>, with a base size of <? echo "$font_size" ?> pt.</p>
Provide a link back to session01.php in case the user wants to change preferences again, and then add some more HTML so that the document is valid:
Save the file with the name session02.php, and place this file in the document root of your web server.
Your entire code should look like this:
<?
//start a session
session_start();
//check for posted values and register defaults
if (($_POST[sel_font_family]) && ($_POST[sel_font_size])) {
$font_family = $_POST[sel_font_family];
$font_size = $_POST[sel_font_size];
$_SESSION[font_family] = $font_family;
$_SESSION[font_size] = $font_size;
//check for stored values, extract from $_SESSION superglobal and register
} else if (((!$_POST[sel_font_family]) && (!$_POST[sel_font_size]))
&& ($_SESSION[font_family]) && ($_SESSION[font_size])) {
$font_family = $_SESSION[font_family];
$font_size = $_SESSION[font_size];
$_SESSION[font_family] = $font_family;
$_SES0SION[font_size] = $font_size;
//register defaults
} else {
$font_family = "sans-serif";
$font_size = "10";
$_SESSION[font_family] = $font_family;
$_SESSION[font_size] = $font_size;
}
?>
<HTML>
<HEAD>
<TITLE>My Display Preferences</TITLE>
<STYLE type="text/css">
BODY, P, A {font-family:<? echo "$font_family"; ?>;
font-size:<? echo "$font_size"; ?>pt;font-weight:normal;}
H1 {font-family:<? echo "$font_family"; ?>;
font-size:<? echo $font_size + 4; ?>pt;font-weight:bold;}
</STYLE>
</HEAD>
<BODY>
<H1>Your Preferences Have Been Set</H1>
<P>As you can see, your selected font family is now <? echo "$font_family";
?>, with a base size of <? echo "$font_size" ?> pt.</p>
<P>Please feel free to <a href="session01.php">change your preferences</a>
again.</p>
</BODY>
</HTML>
Unless you closed your web browser between the last script and now, you should still be staring at the font family and font size selection form.
Select sans-serif for the font family.
Select 14pt for the base font size.
Click on the Set Display Preferences button.
The page is displayed using your selected font family and base font size, and the changes are confirmed.
This is getting fun! With your web browser still open to the confirmation screen for the initial preference changes, click on the change your preferences link.
The selection form is also displayed using your new font family and base font size. Then, follow these steps:
Select Courier for the font family.
Select 8pt for the base font size.
Click on the Set Display Preferences button.
The page is displayed using your selected font family and base font size, and accompanying text also indicates the changes have been made. Again, click on the change your preferences link to see that the selection form is displayed using your new font family and base font size.
Continue changing the font family and sizes, and you'll quickly discover which preferences you like and which are simply annoying! Play around with the script, changing the fonts to others available on your system, or even add other font attributes to your form and accompanying style sheet, such as italic, bold, underline and so forth. Although the purpose of this script is to get a feel for how user-specific elements can be stored in session variables, you can also take the opportunity to explore more about the dynamic display of content.