Setting and Reading Cookies in PHP
📖 Scenario: You are building a simple website that remembers a visitor's favorite color using cookies. When the visitor selects a color, the website saves it in a cookie. When the visitor returns, the website reads the cookie and shows the favorite color.
🎯 Goal: Create a PHP script that sets a cookie named favorite_color with a specific value, then reads and displays the cookie value if it exists.
📋 What You'll Learn
Create a cookie named
favorite_color with the value blueSet the cookie to expire in 1 hour
Check if the cookie
favorite_color existsDisplay the message
Your favorite color is: blue if the cookie existsDisplay
No favorite color set if the cookie does not exist💡 Why This Matters
🌍 Real World
Cookies are used on websites to remember user preferences, login status, and other information to improve user experience.
💼 Career
Web developers often need to set and read cookies to manage sessions, personalize content, and track user behavior.
Progress0 / 4 steps