A cookie in PHP is a small file with a maximum size of 4KB that the web server stores on the client computer. They are typically used to keep track of information such as a username that the site can retrieve to personalize the page when the user visits the website next time.
A cookie can only be read from the domain that it has been issued from.
Cookies are usually set in an HTTP header but JavaScript can also set a cookie directly on a browser.
Setting Cookie In PHP
To set a cookie in PHP, the setcookie() function is used.
The setcookie() function needs to be called prior to any output generated by the script otherwise the cookie will not be set.
Syntax
Parameters - The setcookie() function requires six arguments in general which are:
Name - It is used to set the name of the cookie
Value - It is used to set the value of the cookie.
Expire - It is used to set the expiry timestamp of the cookie after which the cookie can’t be accessed.
Path - It is used to specify the path on the server for which the cookie will be available.
Domain - It is used to specify the domain for which the cookie is available.
Security - It is used to indicate that the cookie should be sent only if a secure HTTPS connection exists
Creating Cookies
Creating a cookie named User_Name and assigning the value Wikitechy to it.
The cookie will expire after 5 days(5 days * 24 hours * 30 mins * 60 seconds).
Sample Code
Output
Checking Whether a Cookie Is Set Or Not
It is always advisable to check whether a cookie is set or not before accessing its value.
Therefore to check whether a cookie is set or not, the PHP isset() function is used.
To check whether a cookie “User_Name” is set or not, the isset() function is executed as follows:
Sample Code
Output
Accessing Cookie Values
For accessing a cookie value, the PHP $_COOKIE superglobal variable is used.
It is an associative array that contains a record of all the cookies values sent by the browser in the current request. The records are stored as a list where the cookie name is used as the key.
To access a cookie named “User_Name”, the following code can be executed.
Sample Code
Output
Deleting Cookies
The setcookie() function can be used to delete a cookie.
For deleting a cookie, the setcookie() function is called by passing the cookie name and other arguments or empty strings but however this time, the expiration date is required to be set in the past.
To delete a cookie named “User_Name”, the following code can be executed.
Related Searches to PHP Cookies - How to Create, Access and Delete Cookies in PHP
session in phpphp cookies and sessionstypes of cookies in phphow to delete cookies in phpdifference between session and cookies in phpphp get cookie by namesession and cookies in php with examplehow to create cookies in phphow to use cookies in php for loginphp cookies vs sessionsphp cookies setphp cookies destroyphp cookies loginphp cookies removehow to clear php cookiesphp session and cookiesphp set cookiesphp get cookiesphp login page with session and cookies examplephp curl cookiesphp clear cookiesphp get all cookiesphp delete all cookiesphp can send and receive cookiesphp delete cookies