In PHP, cookies are small pieces of data sent from a server to a user's browser and stored on the user's device. Cookies are commonly used for various purposes, such as session management, user preferences, and tracking.
The `setcookie()` function is used to set a cookie in PHP.
<?php
setcookie(name, value, expire, path);
?>
<?php
//set a cookie
setcookie("username", "Raj", time()+1800, "codersmile.com");
?>
This example sets a cookie named "username" with the value "Raj," valid for half hour, accessible across the entire domain.
The $_COOKIE superglobal variable used to get cookie in PHP.
<?php
$value = $_COOKIE["name"];
?>
If you set the expiration time, then cookie automatically deleted.
<?php
//cookie will be deleted after half hour
setcookie("username", "raj", time()-1800);
?>