The Set-Cookie header is used to send cookies from the server to the browser. Cookies can store user session data and other information. Secure attributes, such as HttpOnly, Secure, and SameSite, help prevent common attacks like cross-site scripting (XSS) and cross-site request forgery (CSRF).
.htaccess file if you use one).Header set Set-Cookie "sessionId=abc123; HttpOnly; Secure; SameSite=Strict"/etc/nginx/sites-available/your-site).add_header Set-Cookie "sessionId=abc123; HttpOnly; Secure; SameSite=Strict";sudo systemctl restart nginxsetcookie("sessionId", "abc123", [
"httponly" => true,
"secure" => true,
"samesite" => "Strict"
]); Use a library like cookie-parser or set cookies directly:
res.cookie('sessionId', 'abc123', {
httpOnly: true,
secure: true,
sameSite: 'Strict'
}); from flask import Flask, make_response
app = Flask(__name__)
@app.route('/')
def set_cookie():
response = make_response("Hello, World!")
response.set_cookie("sessionId", "abc123", httponly=True, secure=True, samesite='Strict')
return response After setting the header, test your website to ensure itโs working:
Set-Cookie header with the correct attributes.Properly configuring the Set-Cookie header ensures that cookies are secure, reducing the risk of attacks like XSS or CSRF. Attributes such as HttpOnly, Secure, and SameSite are critical for safeguarding user data and maintaining trust.