What are the potential security risks of using cookies for autologin in PHP?
Using cookies for autologin in PHP can pose security risks such as cookie theft, session hijacking, and cross-site scripting attacks. To mitigate these risks, it is important to encrypt sensitive information stored in cookies, use secure HTTPS connections, and implement proper validation and authentication checks.
// Set a secure and encrypted cookie for autologin
$token = bin2hex(random_bytes(32)); // Generate a random token
$hashed_token = password_hash($token, PASSWORD_DEFAULT); // Hash the token
setcookie('autologin_token', $hashed_token, time() + 3600, '/', 'example.com', true, true); // Set the cookie with secure and HttpOnly flags