What are best practices for using secure cookies?

secure cookies, best practices, HTTP-only, secure flag, same-site cookies, web security
This article covers best practices for using secure cookies to enhance web security. Learn how to implement HTTP-only, secure flags, and same-site cookies effectively.

        // Setting a secure cookie example in PHP
        setcookie("user_token", $token, [
            'expires' => time() + 3600,  // 1 hour expiration
            'path' => '/',
            'domain' => 'yourdomain.com', // adjust this to your domain
            'secure' => true,              // Ensure the cookie is sent over HTTPS
            'httponly' => true,            // Prevent JavaScript access to the cookie
            'samesite' => 'Strict'         // Use 'Lax' or 'Strict' to mitigate CSRF attacks
        ]);
    

secure cookies best practices HTTP-only secure flag same-site cookies web security