PHP Cookies

PHP Cookies Tutorial


PHP cookies are a small piece of information or small file with the maximum size of 4KB which is stored at the client browser.
 
Tracking some information of the user in the cookie browser for example what the user is searching for, the user's username, password, how long the user stays on the website, how many times the user visited the website. Multiple cookies are also set.
when the user goes to some set cookie websites, such as e-commerce websites, when the user visits these websites or does some search, then those websites have any set cookies on the user's browser. Stores it on the user's browser. When the cookie is stored, then what activities the user does on that website, it tracks all the information through the websites. Then the recommended information on those websites is communicated to the user through the websites.
For example, when the user has seen any information on any website, then any information related to it is visible on the website of the user, it is all due to cookies.
 
The cookies of some websites are trusted and some cookies are risky which come from third-parties who track some personal information from the user. It would be better for the user not to access the third-party cookies of his browser.
 
Syntax :
       setcookie(name, value, expiry, path, domain, security);
 
Example for Set Cookie(setcookie())
In the example, in the setcookie() function, the name of the cookie is set as 'cookie_name'. The value of cookie 'cookie_value' is set. Cookie expiry is given till 1 day (60(sec)*60(min)*24(hour)).
Here 'path', 'domain', 'security' default will be set in setcookie()
Output : Cookie is set.

 

Accessing Cookie in PHP:
$_COOKIE This superglobal is used to retrieve the cookie.

Output :Cookie Value : cookie_value

To check cookie is set or not :

Output :Cookie Value is cookie_value

Deleting Cookie in PHP :
The setcookie() function is used to delete the cookie. Only the name of the cookie to be deleted and the time elapsed is given.

Output : Cookie is deleted