In the world of websites and web based applications a cookie is a packet of user specific information sent from the server to the client machine. This information is transferred back to server whenever a user opens the website again. Let’s see how PHP setting cookies is implemented practically.
Transfer of cookies
The transfer of cookies between server and client according is handled as per HTTP. They can be understood as visiting cards that let a website identify a client and what kind of conversation client machine has been doing with a specific website.
Cookies allow a website server to save the information with client machine to handle statelessness of HTTP. PHP setting cookies process is very simple and easy. There are two ways in PHP for setting cookies.
Two ways of PHP setting cookies
Manipulating HTML header with PHP header()function
The simple way to set a cookie is by manipulating HTTP header. This is done by setting values of cookie parameters in the PHP header() function. The below example explains it.
<?php header("Set-Cookie: email@example.com; path=/; domain=csveda.com"); ?>
Using PHP setcookie() function
Cookies can be set by calling the setcookie() function and passing the components of cookies discussed in the previous post. PHP cookie manipulation is a single function API that provides both the tasks – setting up a cookie and removing it.
The setcookie function is called by passing 6 argument values. The first one is cookie name and the second one is the value to be stored for cookie in user’s system. Both these are mandatory. If you define a cookie with only these two arguments, it will be sufficient for its setting.
The remaining arguments are expiry, the path , the domain to which a cookie belongs to, boolean value secure whether to allow access only with HTTPS. These all are not mandatory. Cookie can be set even if the setcookie function doesn’t have values for these.
Setcookie function Syntax
setcookie(name, value, expire, path, domain, secure);
With this function server running a script sets the cookie in user system. The location will be as explained in the first part of Cookie tutorial. If the browser is able to send the cookie header to the user’s system it returns true. Transmission of cookie is different from setting it in the cookies file.
If the user has set the browser to reject all the cookies, the cookies may be transmitted. Transmission of cookies does not mean that they are set.
Example of PHP setting cookies with setcookie() function
Here is an example to demonstrate how to set a cookie using PHP setcookie() function.
<?php setcookie('email','firstname.lastname@example.org'); ?>
The above code creates a cookie with name email.
You can set more than one cookies with one setcookie() function for each cookie.
<? setcookie('user','admin', time()+3600*24*365, '/','blog.csveda.com', FALSE); setcookie('email','email@example.com', time()+3600*24*30, '/phpTutorial/','csveda.com', TRUE); setcookie('password','helloCSveda', ,, TRUE); ?>
One PHP setting cookies script is used to transmit three cookies. The first one stores information about user with expiry duration of one year and sub-domain blog of csveda.com.
The second cookie is email. It can be accessed in path phpTutorial of root directory of csveda.com domain. It is transferred when the connection is secure. The last argument is set to TRUE .
The third cookie saves the password value only when connection is secure. You can skip values for other arguments which you don’t want to set.