August 17, 2024
Cookies management in PHP
PHP Cookies: Cookies are use to store persistent data at client side or you can say that on the browser. Cookies are developed by Netscape to store state related and other information on the browser in a persistent manner. Whatever information we store in the cookie it remains in the browser till the browser session do not get close; this cookie information can maintain even a user switch off his / her computer. Even CGI programs can also store information on browser through cookies. Cookies are supported by all major browsers these days. Through cookies you can develop more powerful web applications.
Through setcookie() function you can set a cookie variable which will be sent along with the rest of the HTTP headers. Similar to other headers, cookies are essentially sent before any output from your script. This means setcookie function has highest priority over any output.
After defining a cookie variable you can access it on your different webpages with $_COOKIES global variable. You can also access cookie variables with $_REQUEST global variable.
Parameters of setcookie() function: setcookie() function requieres 7 parameters. Apart from name parameter all the remaining parameters are optional. All the parameters are mentioned below:
name parameter: It is a first parameter. With this you can set name of cookie variable.
value parameter: You can assign a value to your cookie variable. This value will be stored on the clients computer or browser; do not store sensitive information in cookies. Suppose if your cookie variable name is "mycookie" then you can retrieved value through $_COOKIE["mycookie"]
expire parameter: You can define the cookie expire time. This takes value in Unix timestamp so you should pass expire time with the time() function plus the number of seconds before you want it to expire. Or you can also use mktime() function. time()+60*60*24*30 will set the cookie to expire in 30 days. If set this parameter as 0 or leave this parameter then your cookie will be expired at the time you close the browser.
path parameter: Through this parameter you can set path of cookie variable where it should be available. Suppose if you set it as "/" then cookie variable will be available within the entire domain. And if you set it as "/myDirectory/" then cookie will only be available within the /myDirectory/ directory and all sub-directories of myDirectory. If you do not set this parameter then default value is the current directory where the cookie variable is being set in.
domain parameter: Whenever you store a cookie in the browser or you can say that local file system, then it stored by the URL of the webpage which sent that cookie. So domain field is used to generalize the domain name on which a cookie should apply.
secure parameter: If you set the secure field then you would be able to send the cookies over a secure connection such as with HTTPS servers. The default value of secure parameter is FALSE programmers can set it as TRUE according to their requirement.
httponly parameter: When this parameter set as TRUE the cookie will be available only through the HTTP protocol. This is a simple mean that the cookie will not be accessible with scripting languages, like JavaScript. Example for setting cookie variable with setcookie() function:
Code
<?php
$value = "myFirstCookie";
setcookie("TestCookieVariable", $value);
/* below expire in 1 hour */
setcookie("TestCookieVariable", $value, time()+3600);
setcookie("TestCookieVariable", $value, time()+3600,
"/~rasmus/", ".example.com", 1);
?>
Example for printing or retrieving the cookie value
Code
<?php
// Print an individual cookie
echo $_COOKIE["TestCookieVariable"];
// Another way to debug/test is to view all cookies
print_r($_COOKIE);
?>