How can the data stored in cookies be securely transferred to session variables in PHP?

When transferring data stored in cookies to session variables in PHP, it's important to ensure that the data is securely handled to prevent any security vulnerabilities. One way to securely transfer the data is by validating and sanitizing the cookie data before assigning it to session variables. This helps to prevent any malicious code injection or manipulation of the data.

// Validate and sanitize the cookie data before transferring it to session variables
if(isset($_COOKIE['cookie_name'])){
    $cookie_data = filter_var($_COOKIE['cookie_name'], FILTER_SANITIZE_STRING);
    $_SESSION['session_variable'] = $cookie_data;
}