The following code shows an Example of a Simple Cookie in PHP.

Basically, cookies are small files that store a short piece of information in browser storage. The following code creates a cookie that stores a string for a period of one year. As can be seen in the code, the secookie() method creates a cookie. It takes three parameters in this example. Although there are more parameters that the setcookie() method takes. However, only the cookie name, which is the first parameter is necessary. All other parameters are optional. For more information visit the PHP manual here.

The second parameter here represents the value of the cookie. While the third parameter refers to the expiry date. Furthermore, the time() function returns the current time. Therefore, time()+365*24*60*60 evaluates to the current time plus 365 days.

<?php
 $cn="C1";
 $data="Welcome to programmingempire.com!!!";
 $ex=time()+365*24*60*60;
 setcookie($cn, $data, $ex);
 if(isset($_COOKIE['C1']))
 echo $_COOKIE['C1'];
?>

Output

Creating a Simple Cookie in PHP
Creating a Simple Cookie in PHP

Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

programmingempire