Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Cr0w

Cr0w Avatar

**
Official Member

84


April 2006
This tutorial will cover how to create, retrieve, and delete PHP cookies.

Overview Of PHP Cookies:

PHP cookies, like any other cookies are used to store information in your browser for quick access later. A downside of cookies is that a user can only retrieve information from a cookie in their browser.


Setting A Cookie:
PHP cookies work much the same as Javascript cookies. You need to set the cookie name, value, and time of expiry.

Cookies can be set to expire up at any time in the future. One year is 31536000 seconds.

Cookies are considered headers, so must be place at the very top of your document, before the <html> tag, ortherwise you will get a parse error.

Example of setting a cookie:

setcookie("User", "Cr0w", time()+3600);

Now, let's break that down.

setcookie(); is the function used for telling your browser that it's making a cookie.

Cookies have two required fields. name and value.

The first spot in the above snippet is the name field, the second spot is the value field.

name is the name of the cookie, and value is the information stored in the cookie.

setcookie("User", "Cr0w", time()+3600);

So you understand the red bit.

Now for the last field. The last field is the expiry field, which simply tells the cookie when to delete itself.

time() gets the current time. +3600 tells it to add 3600 seconds to the current time.

Cookies can also get data from a form, using $_POST or $_GET.

Example:

setcookie("user", $_POST['username'], time()+31536000);

The above would most likely be used for a login script.

Retrieving Cookies:
Cookies are retrieved using $_COOKIE, much like $_POST and $_GET.

Cookies are retrieved like this:
echo $_COOKIE['User'];

The above will display Cr0w if you used the example used earlier on.

$_COOKIE tells the browser to get the cookie.

['User'] tell the browser to get the cookie named "User".

Note: print_r($_COOKIE); returns an array of all cookies relevant to the page you're on.

Deleting Cookies:
Deleting cookies is simple. It's just like creating a cookie, except you need to set the expiry to a time that's already passed. Like this:

setcookie("User", "Cr0w", time()-3600);

You should understand most, if not all of that snippet. Everything is the exact same, except for the time, you're doing -3600 instead of +3600


There are more fields that you can add in cookies, but i'll cover those in a later tutorial. :)


Last Edit: Oct 12, 2006 20:50:46 GMT by Cr0w
CT Graphics Coding & Design

Mithras

Mithras Avatar

****
Studio Member

600


July 2006
Good tutorial... much easier than Javascript I must say.

Now I can say I learned something today!


Support Rob Scuderi, the #1 Penguins Defender!

lucifer said:
"Behold: me! I have authority in this building."

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
You sure a cookie can't be set for more then one year? I've got cookies set to expire in '09 and '37. ;) (And yes, they're from PHP.)


Last Edit: Oct 12, 2006 0:46:50 GMT by Chris

Cr0w

Cr0w Avatar

**
Official Member

84


April 2006
cddude229 said:
You sure a cookie can't be set for more then one year? I've got cookies set to expire in '09 and '37. ;) (And yes, they're from PHP.)


I asked GAMEchief if there was a limit, and he said a year :P

I'll change it ;) :P

mithras said:
Good tutorial... much easier than Javascript I must say.

Now I can say I learned something today!


Thanks, it's my first tutorial :P(was always too lazy to write them ;))

I agree though, they're much more straightforward than Javascript cookies. :)


Last Edit: Oct 12, 2006 20:52:14 GMT by Cr0w
CT Graphics Coding & Design

newBookmarkLockedFalling