What is the correct way to set and read cookies in PHP?

When setting cookies in PHP, it's important to use the setcookie() function with the correct parameters such as the cookie name, value, expiration time, path, domain, and secure flag. To read cookies, you can use the $_COOKIE superglobal array to access the values of cookies that have been previously set.

// Set a cookie with a name of 'user' and a value of 'John Doe' that expires in 1 hour
setcookie('user', 'John Doe', time() + 3600, '/');

// Read the value of the 'user' cookie
if(isset($_COOKIE['user'])) {
    echo 'Hello ' . $_COOKIE['user'];
} else {
    echo 'Cookie not set';
}