Using a Cookie
Explore how to use cookies in PHP to personalize web pages by storing and accessing user data. Understand how to handle undefined cookies to prevent errors, and learn about cookie settings that affect security and functionality. This lesson equips you with practical skills to manage cookies effectively for dynamic PHP applications.
We'll cover the following...
Array of key-value pairs
The PHP server has a built-in mechanism that takes the Cookie header from the request, processes its contents, and produces a nice array of key/value pairs.
The array is a superglobal variable, just like $_GET and $_POST.
This one is called $_COOKIE.
Let’s use the name cookie in random.php to personalize the page a bit:
Reload the page a few times until it shows a random number higher than 5, after which you should see your name on the screen.
Let’s see how it works ...