PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

WHAT ARE COOKIES AND HOW TO USE THEM IN PHP

Cookies are little files that are stored on a user's computer by the server. They are usually used to identify a user. When you browse to a site that has stored a cookie again, your web browser also sends the cookie.

An example of such usage is the "remember me" feature you see on some sites that you login. If you choose to be remembered, the site stores a cookie on your computer with your username or email. When you visit that site again, the site will retrieve your details from the cookie and maybe display a personalised welcome message or prefill the username/email text input box on the login form with your username or email.

To create a cookie, you use the setcookie() function in PHP. Note the the setcookie() function must come before anything else on a page, even before whitespace or html. This function takes seven parameters in it's advanced form.

The general format is:

setcookie(name, value, expiry, path, domain, secure, httponly);

Name: This is the name of the cookie.
Value: The value of the cookie. Optional.
Expiry: The time the cookie expires. Optional.
Path: The path on the server that the cookie will be available on. If set to / or left out, then it will available in entire directory. To specify a directory, example usage as: /mack/ to make cookie available to mack directory and all sub-directories beneath it eg: /mack/images/. Optional.
Domain: Domain that the cookie is made available. Optional.
Secure: Set to true if cookie must only be sent over secure HTTPS connection, false if not. Default is false. Optional.
httponly: Setting to true indicates cookie only availabe through the HTTP protocol. Default is false. Optional.

However for most progamming tasks, the following simplified option is more than sufficient:

setcookie(name, value, expiry);

Other parameters being left out will take default values.

The following code will set a cookie to a user's computer that only expires in one year.

<?

setcookie("email","john@somesite.com", time() + 31536000);

?>

We have just set a cookie named email with value of john@somesite.com that will expire in one year. A cookie, once set, will be available on the next page load.

The expiry date is calculated as follows:

1 (year) X 365 (days) X 24 (hours) X 60 (minutes) x 60 (seconds) = 31536000

Then add 31536000 to the current timestamp time().

Let's assume that we want to retrieve this cookie and it's value. A cookie can be retrieved using the $_COOKIE variable as follows:

<?

$_COOKIE['email'];

?>

If we echo the $_COOKIE['email'] above, we will get a value of john@somesite.com.

If you want to delete a cookie, you simple set the expiry time to the past as follows:

<?

setcookie("email","john@somesite.com", time() - 31536000);

?>

Home | Privacy Policy | Terms Of Use | Contact Us