Tutorial on PHP Cookies & Sessions

February 21, 2009, 11:32 pm by Rob Johnson
Tutorial on PHP Cookies & Sessions

So I thought I would post a little tutorial on PHP cookies and sessions just to help out those who enjoy to learn as much as I have and with that said, let us begin!

Cookie Time!

Cookies

A cookie is a file that is stored client-side and is initiated by back-end code of a website. A cookie can store any small sized piece of information from usernames, passwords and even how many times you have visited a particular site.

Cookies, in PHP, are created using the setcookie() function. The values that are contained within the parenthesis are, name of the cookie, value of the cookie, the date the cookie expires, path of where the cookie can be located on the server, domain where the cookie is available and where it is coming from a secure page.

However the only values that really concern a programmer (at least initially are) $name $value and $expired. Below is an example of how to code a cookie in PHP.


<?php
setcookie("name", "Rob", time()+3600);
?>

To retrieve a cookie we simply use echo the name we assigned to the cookie.


<?php
echo $_COOKIE["name"];
?>

A way that I would prefer to make this happen is by assigning the first chunk of code to a variable name like this.


<?php
$namecookie = echo $_COOKIE["name"];
?>

Now I would be able to echo that variable name anywhere on a page as many times as I would like and not have to worry about bulking up code or being repetitive in back-end programming. To call this variable we simply do this.


<?php
echo "$namecookie";
?>

You would want to make sure that you use double quotes otherwise using a variable in a string with single quotes would result in ‘$namecookie’ and not the value of the variable.

To delete a cookie we set call the cookie function as if you were creating the cookie again, however this time we set the expiry date to a previous date, this can be done by simply applying a minus before the amount of seconds.


<?php
setcookie("name", "Rob", time()-3600);
?>

Sessions

Sessions seem to be a recent trend in recent years as they do not store information on client-side and all information is deleted once the user leaves the website. Therefore sessions are not permanent and last as long as the user is viewing your site. This is helpful for shopping carts, keeping a user logged in and preferences.

To start a sessions we use the following code before any HTML tags.


<?php
session_start();
$_SESSION['name'] = Rob;
?>

To recall a session variable use this code.


<?php
session_start();
echo "Your name is ".$_SESSION['name'];
?>

Deleting a session is called “cleaning” and that is done by using the following code.


<?php
session_start();
unset($_SESSION['name']);
?>

Another way to end the recall of session information is by DESTROYING sessions. This is usually used when people logout and is implemented by this code.


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

And there you have it, you can create, delete, destroy both sessions and cookies!


About the Author:  Rob Johnson is a freelance web developer who has a passion for online marketing. He is an Addy Award winner and currently teaches a New Media Strategies class at BYU-Idaho.


Leave a Reply