Setting Up Cookies by Using PHP
Internet cookies are as old as the browser itself, cookies are used to track and temporary store data on a person’s browser while web surfing through various websites, especially websites that require a person to log in or input search engine data. Of course, with the help of PHP you can actually design scripts to set up cookie data, and this is most evident when your shopping online looking at products, purchasing products or creating a new account.
So why the need for cookies? Simple without them you wouldn’t be able to backtrack in your browser history folder and thus you would have to retype the web address or click on hte site with your favorites folder. So on that note lets take a look on how to program some cookies.
So the first part to setting up the cookie php script is first to give it a function, and in this case that function is called setcookie()
To take it a set further on how a cookie will look like take a look at the following example:
setcookie('name of cookie', 'value of the cookie');
It is interesting to note that you can also use variables with in your cookies and it would look something like this:
setcookie('$name_of_cookie', '$value_of_the_cookie');
On that note lets get to our first script in which we will we will set up a Font Size cookie and Font Color cookie.
So the first thing we are going to do is create our form in which we will be using $POST function to post the data into the cookie file that the PHP script creates.
<form action="customize.php" method="post"> <select name="font_size"> <option value="">Font Size</option> <option value="xx-small">xx-small</option> <option value="x-small">x-small</option> <option value="small">small</option> <option value="medium">medium</option> <option value="large">large</option> <option value="x-large">x-large</option> <option value="xx-large">xx-large</option> </select> <select name="font_color"> <option value="">Font Color</option> <option value="999">Gray</option> <option value="0c0">Green</option> <option value="00f">Blue</option> <option value="c00">Red</option> <option value="000">Black</option> </select> <input type="submit" name="submit" value="Set My Preferences" /> <input type="hidden" name="submitted" value="true" /> </form>
So We have the form all set up and now lets digg into the setting up the PHP using the setcookie() function. But before I get to that, I would like to mention that there is a specific way for cookie scripts to be set up and that those specific scripts need to go ahead of the HTML document, meaning that your PHP file will have the cookie script first then the HTML code after it, due to the face you don’t want to cookie data to save the HTML code and what not.
// Handle the form if it has been submitted:
if (isset($_POST['submitted'])) {
// Send the cookies:
setcookie('font_size', $_POST['font_size']);
setcookie('font_color', $_POST['font_color']);
// Message to be printed later:
$msg = '<p>Your settings have been entered! Click <a href="view_settings.php">here</a> to see them in action.</p>';
} // End of submitted IF.
<!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>Customize Your Settings</title>
</head>
<body>
// If the cookies were sent, print a message.
if (isset($msg)) {
print $msg;
}
So it would look something like that and of course you would have that form we set up right below the last PHP script. Ok so lets take a look at the PHP right quick.
Our goal for this script is to store cookie data based on some user options and sothe first part check to see if something was submitted it will run the script and then give you a message saying the data has been stored and that is done by using if (isset($_POST['submitted']))The next part is the actually cookie data we want to post and so the data we want to have posted is Font size and Font Color and so we give it a name and we give it a value. Next we have the message and after that we end the script by saying if the data has been submitted end the script.
FULL SOURCE CODE
// Script 9.1 - customize.php
// Handle the form if it has been submitted:
if (isset($_POST['submitted'])) {
// Send the cookies:
setcookie('font_size', $_POST['font_size']);
setcookie('font_color', $_POST['font_color']);
// Message to be printed later:
$msg = '<p>Your settings have been entered! Click <a href="display.php">here</a> to see them in action.</p>';
} // End of submitted IF.
<!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>Customize Your Settings</title>
</head>
<body>
// If the cookies were sent, print a message.
if (isset($msg)) {
print $msg;
}
<p>Use this form to set your preferences:</p>
<form action="customize.php" method="post">
<select name="font_size">
<option value="">Font Size</option>
<option value="xx-small">xx-small</option>
<option value="x-small">x-small</option>
<option value="small">small</option>
<option value="medium">medium</option>
<option value="large">large</option>
<option value="x-large">x-large</option>
<option value="xx-large">xx-large</option>
</select>
<select name="font_color">
<option value="">Font Color</option>
<option value="999">Gray</option>
<option value="0c0">Green</option>
<option value="00f">Blue</option>
<option value="c00">Red</option>
<option value="000">Black</option>
</select>
<input type="submit" name="submit" value="Set My Preferences" />
<input type="hidden" name="submitted" value="true" />
</form>
</body>
</html>
Now we throw in our HTML and then we use another PHP to say if the data has been submitted print that message.
Don’t forget to renter the PHP tags in order to get the script to work.













Leave your response!
You must be logged in to post a comment.