How can PHP interact with cookies to store user data securely?

To interact with cookies securely in PHP, you can use built-in functions like setcookie() to set a cookie with user data and $_COOKIE to retrieve the data. To ensure security, you should always sanitize and validate user input before storing it in a cookie to prevent injection attacks. Additionally, you can encrypt sensitive data before storing it in a cookie to add an extra layer of security.

// Set a cookie with user data
$userData = 'John Doe';
setcookie('user', $userData, time() + (86400 * 30), '/', '', true, true);

// Retrieve the user data from the cookie
if(isset($_COOKIE['user'])){
    $userData = $_COOKIE['user'];
    // Do something with the user data
}