Home » Featured, PHP Tutorials, Tutorials

Reading Cookies in PHP

30 March 2009 71 views No Comment


So if you were to run that script in the last example, and then click on the new link that the message creates you should get a not found message, and so now we are going to set up a PHP script to be able to ready that sotred cookie data.

So how does one view cookie data? Well you could view the cookie files through your browser and unless you been dealing with cookie data all your life it is going to be one complete file of gibberish. However, with the help of PHP it will un-jumble that gibberish and make it a bit more organized. So we will be setting up a set of if else statements in order to display that cookie day by setting it up like this.

// Check for a font_size value:
if (isset($_COOKIE['font_size'])) {
print "ttfont-size: " . htmlentities($_COOKIE['font_size']) . ";n";
} else {
print "ttfont-size: medium;n";
}

// Check for a font_color value:
if (isset($_COOKIE['font_color'])) {
print "ttcolor: #" . htmlentities($_COOKIE['font_color']) . ";n";
} else {
print "ttcolor: #000; n";
}

Now to break this script down as best I can, I will point out a few things, the first of which is $_COOKIE and its function is just like $POST and $GET but this function pretty much only gets cookie data.

The first part of the script is that it will retrieve any data that has been stored within font_size and the same goes with font_color. Now that is the IF statement, however, if no cookie data exist it will print out a default setting and in this case, the defaults that will be displayed will be medium and #000. Now within that if/else statement there is another function called htmlentities() and the primary purpose for this function is for security purposes. With the use of this function it prevents altered cookie data from being displayed because cookie data can be manipulated to do something else, but I won’t get into that something else.

FULL SOURCE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>View Your Settings</title>
	<style type="text/css">
		body {
--BEGINNING OF PHP SCRIPT--
// Script 9.2 - view_settings.php

// Check for a font_size value:
if (isset($_COOKIE['font_size'])) {
	print "ttfont-size: " . htmlentities($_COOKIE['font_size']) . ";n";
} else {
	print "ttfont-size: medium;n";
}

// Check for a font_color value:
if (isset($_COOKIE['font_color'])) {
	print "ttcolor: #" . htmlentities($_COOKIE['font_color']) . ";n";
} else {
	print "ttcolor: #000; n";
}

--END OF PHP SCRIPT---
		}
	</style>
</head>
<body>
<p><a href="customize.php">Customize Your Settings</a></p>
<p><a href="reset.php">Reset Your Settings</a></p>

<p>
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
yadda yadda yadda yadda yadda
</p>

</body>
</html>

Leave your response!

You must be logged in to post a comment.