CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Library Index]

[View category: PHP] [Discuss Article]

Cookies in PHP

Article is yet to be rated
Author:      Rae
Submitted:      28-Apr-2007 19:41:03
Imported From:      The CyberArmy University (original author: rae)


Cookies in PHP
Cookies are small text files stored on the client's computer and are useful in authentication of the user. For a detailed explanation of how cookies work, read up RFC 2965. Take a look a the following code in PHP,
<tt>setcookie('mycookie', $name);</tt>
This is the PHP 'setcookie()' function and is used to create a cookie. The first parameter is the string name of the cookie while the second parameter is the value that you want to store in the cookie. Since we haven't set the lifetime of the cookie, it will expire when the user closes the browser. The expiration date of the cookie is expressed as a Unix format timestamp plus the number of seconds. To give a customised expiration date to a cookie, we use,
<tt>
setcookie('mycookie', $name, $cookie_life);

setcookie("mycookie", $name, time()+3600);
</tt>
The first line shows how to use a variable to set the expiration time, but this format is rarely used. The second statement is more useful since it sets the expiration time to the current time plus one hour (3600 seconds). Now remember that setcookie() is actually a function, and its return type is 'bool'. In plain english, this means that if the function successfully executes, it return 'TRUE', else it returns a 'FALSE' value.
The other parameters in the 'setcookie' function are 'path', 'domain' and 'secure'. The 'path' specifies the address on the sever where the cookie is available. 'Domain' specifies the domain and subdomains on which the cookie is made available, and 'secure' specifies if the cookie is available only on HTTPS connections. To do this, the parameter has to be set to the value '1'.
The following script is used to check whether the cookie has already been set or not. Its always a good practice to do this before setting or initialising the cookie.
<tt>
<?php
// check if cookie is already set
if (!isset($mycookie)) {

setcookie("mycookie", $name);

}
?>
</tt>
To delete a cookie, we have to use the same function 'setcookie()'. The only difference here being that the value field should be kept null, that is it should be an empty string. Remember that the other parameters should be kept exactly the same, only the value field will change. Take a look at the code below,
<tt>
//This creates the cookie

setcookie("mycookie", $name);
//This deletes the cookie

setcookie("mycookie", "");

</tt>
This brings us to the end of the article. Remember that this is just a brief introduction to cookies. There is a lot more to be learnt about them, I just hope this served as a good beginning. If you want to learn more about cookies, my first suggestion would be http://www.php.net/setcookie. This is the official PHP manual reference, and is very informative.

References :-

http://www.free2code.net
http://www.codewalkers.com
http://www.phpfreaks.com

This article was originally published by CyberArmy.net in the CyberArmy Library.

You must be logged in to vote on an article

About Us | Privacy Policy | Mission Statement | Help