Developing a User Personalization System with PHP and Cookies - Reading from Cookies (
Page 4 of 5 )
Cookies are
an easy, transparent way to store data on a user's computer, and to retrieve
that data every time the user returns. Used wisely so they don't store sensitive
data, they can allow seamless interactivity for any web application.
To create a cookie on a user's computer, use the following syntax in PHP3:
setcookie(cookie name, value, expire time, string path,
string domain, secure flag);
The rules for using this function are:
- You must declare the cookie before the HTML header in your script, since it
is sent as part of the header. That means before the <HTML> tag.
- You can skip entries with "", and leave off the last entries if not needed.
- Just using "setcookie(cookie name)" or setting the expiration time
to 0 will delete the cookie off the user's system.
- The secure flag is a 0 or 1, and identifies whether the cookie should be
transmitted over an SSL connection.
- Expire time is expressed in seconds, and normally defined with
time()+ a number of seconds, such as time()+86400, if the
cookie should expire in 24 hours.
Values saved in cookies by a particular site are automatically sent to that
site in every header. PHP3 automatically decodes the variable and assigns it to
a variable, so to access its value in PHP3, just use the variable as you would
normally -- it already exists. For example, we can grab the cookie and print out
the customized page we have been building for this user. The following script
will grab the appropriate row from the database, and print out the page with all
of their settings.
<?
if ($visitor){
mysql_connect("localhost", "username", "pass")
or DIE("Unable to connect to database");
@mysql_select_db("project") or die("Unable to select database");
$result = mysql_query("select * from users where login='$visitor'");
$row = mysql_fetch_array($result);
?>
<HTML>
<HEAD><TITLE>Welcome to your headlines!</TITLE>
</HEAD>
<BODY>
<H1><CENTER>Welcome <? echo $visitor; ?>!</CENTER></H1>
<br><br>
The news for today is:<br>
<table> <tr>
<td><? include ("news/$row[news1]"); ?></td>
<td><? include ("news/$row[news2]"); ?></td>
<td><? include ("news/$row[news3]"); ?></td>
</tr>
</table>
<?
}
else { ?>
<HTML>
<HEAD>
<TITLE>Login now!</TITLE>
</HEAD>
<BODY>
<br>Choose the headlines you would like to see!<br>
<form method=post action="newuser.php3">
<br>
Please pick a login name:<input name="login" type="TEXT" value="">
<br>
Now pick a password:<input name="password" type="TEXT" value="">
<br><br>
<br>News Choice 1:<SELECT NAME="news3" size=5 value="">
<option value="slashdot.lnk">Slashdot
<option value="freshmeat.lnk">Freshmeat
<option value="voodooextreme.lnk">VoodooExtreme
<option value="devshed.lnk">DevShed
<option value="bluesnews.lnk">Blue's News
</select>
<br><input type="submit" name="Submit" value=" Submit ">
<br></FORM>
<? } ?>
</BODY>
</HTML>
This script branches between two different possibilities: the first section
is executed if the user has a cookie set, at which time their persoanl settings
are retrieved and used to build a page, while the last part of the script is
executed when no cookie is set (this is the same form used on the last page). By
making this our index file, users will automatically get the page intended for
them.
PHP3 does all the dirty work, automatically reading the headers sent from the
client and placing the cookie in a variable. Note how the $visitor
variable will have a value and be true if the cookie has been set. Another way
to test for a cookie is with the "isset" command, as in:
if (isset($visitor)){
PHP3 does all the dirty work, automatically reading the headers sent from the
client and placing the cookie in a variable. Nothing could be easier!
If a cookie has been set, the script reads its value, grabs the appropriate
row from the database, and includes the appropriate headlines. Since values
stored for news choices are named the same as the filenames they are stored in,
putting them into the page is as easy as:
include ("news/$row[news1]");
(Remember, we put those headlines in the "news/" subdirectory.)
The only other bit of code in this script is where the user's login name is
printed out in a welcome message:
Welcome <? echo $visitor; ?>!
For an even briefer version for the same results, PHP3 will accept the ASP
standby of:
<%= $visitor %>
As a matter of fact, it is only lightly documented, but the <? and ?>
tags are interchangeable with the ASP tags <% and %>.