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

[PHP] Cookies in PHP


[Reply] [View by Thread] [Help]
[Back To Article Discussion Forum]

Posted by Author Rae On 2007-04-29 10:02:33




View and vote on the article here: Cookies in PHP


Cookies in PHP

Category
PHP
Summary
Body
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 explaination 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 :-

<a href>http://www.free2code.net[/url]

<a href>http://www.codewalkers.com[/url]

<a href>http://www.phpfreaks.com[/url]


Written by Rae

-----

{BACKLINKS(info=>hits|user,exclude=>SandBox,include_self=>0,noheader=>0)}{BACKLINKS}


This article was imported from the CyberArmy University site. (original author: rae)


There are no replies to this post yet.



Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.08128 seconds


About Us | Privacy Policy | Mission Statement | Help