How to Get a Cookie in PHP?

You can get the value of a cookie by its name via the $_COOKIE superglobal array, like so:

$_COOKIE['cookie_name'];

Using the $_COOKIE array you can access all cookies (by their name) that are passed to the current PHP script via HTTP cookies. For example, if you have a cookie called "foo", you can access it like so:

$_COOKIE['foo'];

Please note that if you have dots (.) or spaces ( ) in the cookie name, then they will be replaced with underscores (_).

Since $_COOKIE is a superglobal, it is available in all scopes throughout your script.


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.