Web Designing WAP development EWAVEZ Web Designs Software Development EWAVEZ Home About Us Contact Us PHP Tutorial
Multimedia
Web Development  
Web Hosting
Multimedia development
Software Development
Web Solutions
Our Clientele
     
 
 

PHP-Tutorial

 
 

PHP Cookies

Another great feature in PHP is cookie handling, PHP lets us do this with ease. The function we use to set a cooke is setcookie().

setcookie('username', $name);

It's as easy as that, the first parameter is the string name of the cookie, this cookie will now be know as username. The second value is the value which you want to store in the cookie, in this case it's a variable $name. The above is the bear minimum, this is the simplest cookie you can set, and this cookie will deleted when the user ends their session (closes their browser). We can define how long we wish a cookie to last:

$cookie_life = time() + 31536000;

 

setcookie('username', $name, $cookie_life);

It's as easy as that. Another function on PHP is time(). This simply returns the current UNIX timestamp, so we call this, and then add 31536000 seconds to it. What is 31536000 seconds? That's right, a year, so this cookie will last for one whole year. That is if the client doesn't delete it or something, anyways, the third parameter is the life of the cookie, how long we want it to last, if this is left undeclared the cookie is deleted when the user's session ends. if we wish to delete a user's cookie we can simply set the life to a time in the past, e.g: time() - 3600. That will set the user's cookie life to one hour in the past, deleting it. Remember, cookies must be deleted with the same parameters they were set with.

We can also use a forth parameter, the fourth parameter is the cookie path.

$cookie_life = time() + 31536000;
$path = '/jesteruk/folder/';

setcookie('username', $name, $cookie_life, $path);

The fourth parameter defines a path from which the cookie can be accessed. In the above example only scripts inside /ewavez/folder/ will be able to access the cookie ($username), in scripts outside this folder the cookie will be unavailable.

Yup, there's a fifth parameter, the fifth parameter defines a hostname that can retrieve the cookie.

 

$cookie_life = time() + 31536000;
$path = '/ewavez/folder/';
$hostname = 'ewavez.com';

setcookie('username', $name, $cookie_life, $path, $hostname);

The fifth parameter is the domain which can retrieve this cookie. Normally a cookie can only be accessed by the domain that set it, otherwise sites would be able to get private information from cookies set by other sites. With this parameter we can define another domain which can retrieve the cookie we are setting.

Retrieving the Cookie

Once you have set a cookie on a user's system, it is only avialable when they load the next page. The cookie can be accessed very easily:

// Our cookie would be contained in

$HTTP_COOKIE_VARS['username']

// or

$_COOKIE['username']

When the cookie is set the value is automatically URL encoded, when the script retrieves a cookie, it automatically decodes the value, so you don't have to worry about that.

Always remember, a limitation of cookies is that you have to set it before you can send the HTTP headers. In other words, always do all your cookie setting BEFORE you output any HTML, otherwise the cookie will not be set and your page will have an ugly error stuck on it.

 

 
Previous Index Next
 
 
 

EWAVEZ Web Designs. Copyright © 2004 All Rights Reserved

| Home | About Us | Contact Us | Terms & Policies | Hosting | Multimedia | Web Designing | Software | Our Clients

 Search