SakhaliaNetHome PageMilitary HistoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Cookies

PHP Example #75

Setting a basic cookie

Cookies store data from the users in their own computers and are managed by their client browsers. This data will be kept in the server during a certain specified time, or until the client browser is closed. In general terms, the instruction to set a cookie has to be placed in the very beginning of a webpage to work. By setting On the configuration directive output_buffering, the declaration of a cookie can be made anywhere in the webpage. In this first example, the cookie's timing is not specified and therefore it is set to default value, this is, until the browser is closed.

The cookie has a value assigned to it (Anonymous User) that will identify a certain user. By using the auto-global array $_COOKIE, we can get the value set in the cookie. This value can be a string or a number, but it must be a simple variable and not an array or other type of complex data structure. The data set in the cookie is not sent to $_COOKIE the first time that the page that sets the cookie loads, but after the server returns the cookie when the first petition is made, so you must reload this page to see the value of the cookie appearing in the message.

Optionally, a lifetime can be set for a cookie; if omitted or set to 0, the cookie expires when the client browser is closed. Time is set in seconds, as in this example, that sets the duration of the cookie for an hour (the time() function returns the number of seconds lapsed since 1 January 1970): <?php setcookie('userid', 'Anonymous User', time() + (60 * 60)); ?>. To set the cookie for a day, it would be simply: <?php setcookie('userid', 'Anonymous User', time() + (60 * 60 * 24)); ?>. Also it can specified a date-time value by using the mktime() function (format hh,mm,ss,mm,dd,yy); the following example sets the cookie until 12:00 PM 1 October 2016: <?php setcookie('userid', 'Anonymous User', mktime(12,0,0,10,1,2016)); ?>.

// The cookie is declared in the very beginning of the webpage
<?php setcookie('userid', 'Anonymous User'); ?>

<?php
// Anywhere in the webpage, we can access the data stored in the cookie
print 'Hello, ' . $_COOKIE['userid'];
?>
Hello,

PHP Example #76

Setting a basic cookie

Usually, cookies are returned only with petitions from pages that are hosted in the same directory where the page that sets the cookie lies. A cookie set for http://www.example.com/buy.php would be returned with all the requests to http://www.example.com, because buy.php lies in a superior level directory. A cookie set for http://www.example.com/catalog/list.php would be returned with a request to any page located in the directory /catalog or in any subdirectory inside it, but it wouldn't be returned with any request from outside the directory /catalog.

The part of the URL after the name of the host (http://www.example.com) is called the path. To tell to the client browser to match a different path when determining if sending or not a cookie to the server, we pass a fourth argument to setcookie(); the most flexible argument that we can use is '/', which means that the cookie will be returned with all the requests made to the server. When a path is specified for the cookie, the expiration date must be specified as well.

The last argument for setcookie() can be set if we want the cookie to be sent with petitions to more than one host, provided that the names of those hosts match with the argument; for example, the argument .example.com would make the cookie available for domains like shop.example.com or testing.development.example.com, and not only for www.example.com.

Example: A cookie set for a specific directoy in example.com

<?php setcookie('userid', 'Ralph', 0, '/catalog/'); ?>

Example: A cookie set for any directory in every host whose name includes the domain example.com

<?php setcookie('userid', 'Ralph', 0, '/', '.example.com'); ?>

// The cookie is declared in the very beginning of the webpage
<?php setcookie('userid2', 'Unknown User', 0, '/'); ?>

<?php
// Anywhere in the webpage, we can access the data stored in the cookie
print 'Hello, ' . $_COOKIE['userid2'];
?>
Hello,

PHP Example #77

Deleting a cookie

To delete a cookie, we just pass an empty string to the value on the cookie. If we have set values that are not default for expiration date, path or domain, those same values have to be passed again to properly erase the cookie. For example, to delete the cookies used in this page, we would use the code shown below.

// Deletes the cookies set for this page
<?php setcookie('userid', ''); ?>
<?php setcookie('userid2', '', 0, '/'); ?>