PHP Cookies Example create Access and Delete
In this article, we will explain to you how to use PHP Cookies with example(PHP Cookies Example create Access and Delete). here we give you a simple PHP cookies example. so you can see the below example.
The PHP cookie is a small file that is stored in the client’s browser. It is used to temporarily store data. we will inform you how to use cookies in PHP and how to set cookies, get cookies, and delete cookies with PHP. so you can see our example.
Set Cookie Example
if you want to set a cookie then you can easily set the cookie using the setcookie() function. for that, you need to pass a three-parameter in this function such as name, value, and expiration time.
1 2 3 4 5 | <?php $cookie_val = 'abc'; //variable setcookie("websiteName", $cookie_val, time()+3600, '/', 'xpertphp.com', true, true); // ?> |
Retrieve Cookie Value
After sending the cookie, you can easily get the cookie using $_COOKIE[‘cookie_name’].
1 | echo 'Your Website Name Is ' . $_COOKIE['websiteName']; |
Updating Cookie Value
If you want to update the cookie then just update the cookie’s value and pass it inside the function. for that, you can see the below example.
1 2 | $cookie_val = 'xyz'; //variable setcookie("websiteName", $cookie_val, time()+3600); |
Delete a Cookie
If you want to delete the cookie then just add a negative expiration time.
1 | setcookie("websiteName", "", time()-3600); |