What is the potential issue with setting and accessing cookies in PHP code?

The potential issue with setting and accessing cookies in PHP code is that they can be vulnerable to attacks such as cross-site scripting (XSS) or cookie tampering if not properly sanitized. To mitigate this risk, it is important to properly sanitize and validate any data being stored in cookies before setting or accessing them.

// Sanitize and validate data before setting cookies
$cookie_name = "user";
$cookie_value = "John Doe";

// Sanitize the cookie value
$cookie_value = htmlspecialchars($cookie_value);

// Set the cookie with sanitized data
setcookie($cookie_name, $cookie_value, time() + 3600, "/");

// Access the cookie with sanitized data
if(isset($_COOKIE[$cookie_name])) {
    $user = htmlspecialchars($_COOKIE[$cookie_name]);
    echo "Welcome back, " . $user;
}