Navigation
Poll
Would you be an active poster if Ron's Guide had a message board?


Total Votes: 69
Comments: 20 — View
Past pollsPoll idea?
Rate Ron's Guide
Rate our resource at Bigwebmaster.com
Cookies via PHP
Mmm... cookies... No, sorry not those kind of cookies. This tutorial will show you how to create the less delicious, but very useful, cookies using PHP.

Cookies allow you to store bits of information on users' computers. This information can be used for many purposes, including remembering settings from a previous visit, login details, and much more.

PHP makes it very easy to use cookies. To set a cookie using PHP we need to use the setcookie function, documented here: setcookie(). Use of this function must be before any other output to the page, including HTML tags, whitespace, etc. If you call the function after there has been other output to the page, PHP will give you a nice warning message and the cookie will not be set.

The setcookie function consists of six parameters. I will explain the first four of these parameters in this tutorial. If you would like information on the last two, visit the documentation link above.

setcookie(name, value, expire, path, domain, secure);

Here is an example code for setting a cookie, try it yourself:

<?php

// set a cookie named 'myName' with a value of 'Ron'
setcookie('myName''Ron');

?>

Accessing Your Cookies

To access cookies once they've been set, simply use the superglobal array $_COOKIE. Try this example code after you've set a cookie using the code above:

<?php

// echo the 'myName' cookie value in a sentence
echo "My name is ".$_COOKIE['myName'].".";

?>

This should print "My name is Ron." to the page.

Cookie Expiration

To have the cookie expire at a certain time we use the third parameter of the setcookie function. The default expiration of a cookie (if you don't set a value for the third parameter) is when the user closes his or her browser window.

The expire parameter expects an integer value, in most cases you would use the time() function and add the number of seconds you want the cookie to expire from the time it is set. The time() function is used to generate the current Unix timestamp. For more information on timestamps visit http://php.net/time.

Here is an example:

<?php

// have the cookie to expire one day from time it is set
// 60*60*24 = 86,400 seconds = 1 day
setcookie('myName''Ron'time() + (60*60*24));

?>

Now our cookie will automatically be removed from the user's computer one day from the time it is set.

Cookie Path

If you want your cookie to only be available within a certain path of your website use the fourth parameter. The default path of the cookie is the current directory from which the cookie is being set.

For example, if you want your cookie to be available to the "guestbook" directory of your site then set the path parameter to "/guestbook/". If you want your cookie to be available within the entire domain, set the path parameter to "/".

Here is some sample code:

<?php

// set a cookie available to the entire domain
setcookie("mycookie""my value""""/");

// set a cookie available only to the directory
// "members" and all of its subdirectories
setcookie("handsoff""my value""""/members/");

?>

That's the basics of setting and accessing cookies. Try setting some of your own cookies with different expirations and paths and further experiment with the setcookie function. Good Luck!

Discuss Tutorial: Cookies via PHP 14 Comments
Comment by Ron on Feb 4, 2006, 8:07 am
Please post questions or comments about this tutorial below. Smile
Comment by Brian on Feb 7, 2006, 10:30 pm
Could I do something like

if($_COOKIE[\'blah\']){
echo \'logged in\';
}

to check if a cookie exists?
Comment by Ron on Feb 7, 2006, 10:50 pm
If you want to check if the cookie exists use the isset function. Like this:

if(isset($_COOKIE[\'mycookie\'])) {
echo \"The cookie exists!\";
}
Comment by Teco on Feb 9, 2006, 4:03 pm
Well play\'ed arround with this tut, and i\'m now using it on my site for my friendNet system to check login cookies... Cool

Really nice Ron Smile
Comment by Bfr on Feb 11, 2006, 2:14 am
Another great tutorial, Ron. It was about time you made another tutorial Tongue
Comment by Ron on Feb 16, 2006, 7:12 am
Excellent, Glad it was helpful Teco. Smile

Thank you Bfr Smile
Comment by Kingjokin on Jul 9, 2006, 1:15 pm
Can i use form variables as perameters for my cookie. like to log a person in directly upon registration?
Comment by Ron on Jul 10, 2006, 5:28 am
Yes, you could do that.
Comment by Kingjokin on Jul 10, 2006, 9:47 pm
allright., how?
Comment by Ron on Jul 11, 2006, 6:45 am
well, you could do something like:

<?php

setcookie('username', $_POST['username']);

?>

That would set the cookie "username" to the value of the username field passed through a post form.

« Previous [ 1 2 ] Next »
Post a comment
Sorry, you must be a registered member to post comments.

If you would like to register, you can do so here.
If you already have an account, please login.