Accessing cookies in PHP
In PHP, you can access cookies using the $_COOKIE
superglobal array. The $_COOKIE
array contains key-value pairs representing each cookie that was sent with the current request. Here’s an example of how to access a cookie in PHP:
// Set a cookie with the name "username" and value "john"
setcookie("username", "john", time() + 3600, "/");
// Retrieve the value of the "username" cookie
echo $_COOKIE["username"];
In this example, the setcookie()
function sets a cookie with the name “username” and a value of “john”. The $_COOKIE
array is then used to retrieve the value of the “username” cookie and print it to the screen.
Note that cookies can only be accessed on subsequent requests, so you may need to refresh the page or navigate to a new page to see the effects of setting or accessing a cookie. Additionally, cookies can only be accessed on the same domain that set them, so you can’t set a cookie on one domain and read it on another.
Apply for PHP Certification!
https://www.vskills.in/certification/certified-php-developer