Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Cookie Flags: Secure, HttpOnly, and SameSite | Working with Cookies in PHP
PHP Sessions and Cookies

bookCookie Flags: Secure, HttpOnly, and SameSite

When you work with cookies in PHP, understanding cookie security flags is essential to protect your users. Three key flags—Secure, HttpOnly, and SameSite—play a vital role in defending against common web vulnerabilities. Each flag controls how and when a cookie is sent or accessed, reducing the risk of sensitive data exposure.

set_secure_cookie.php

set_secure_cookie.php

copy
1234567891011121314151617
<?php // Set a cookie with Secure, HttpOnly, and SameSite flags setcookie( "user_token", "abc123secure", [ "expires" => time() + 3600, // 1 hour from now "path" => "/", "domain" => "", // current domain "secure" => true, // only send over HTTPS "httponly" => true, // not accessible via JavaScript "samesite" => "Strict" // only sent for same-site requests ] ); echo "Secure cookie set with Secure, HttpOnly, and SameSite=Strict flags."; ?>

In the code above, you set a cookie named user_token with all three security flags enabled. The Secure flag ensures the cookie is only sent over HTTPS connections, protecting it from being intercepted on unencrypted networks. The HttpOnly flag prevents JavaScript on the page from accessing the cookie, blocking many cross-site scripting (XSS) attacks. The SameSite flag, set to "Strict", tells browsers to only send the cookie for requests originating from the same site, reducing the risk of cross-site request forgery (CSRF).

Each flag contributes to a layered defense:

  • Secure: only transmits the cookie over encrypted HTTPS connections;
  • HttpOnly: hides the cookie from client-side scripts;
  • SameSite: restricts when the browser sends the cookie, controlling cross-site transmission.

If you omit these flags, your cookies may be exposed to various attacks. Consider what happens when you set a cookie without these protections.

set_insecure_cookie.php

set_insecure_cookie.php

copy
123456
<?php // Set a cookie without Secure, HttpOnly, or SameSite flags setcookie("user_token", "abc123insecure", time() + 3600); echo "Insecure cookie set without Secure, HttpOnly, or SameSite flags."; ?>

Comparing the two approaches, cookies set with all three flags are much better protected. The first example ensures cookies are only sent over secure channels, hidden from scripts, and limited to same-site requests. The second example leaves cookies exposed to interception, script access, and cross-site attacks. Always use these flags for any cookie that stores sensitive or authentication-related data.

question mark

Which of the following statements best describes the combined effect of setting Secure, HttpOnly, and SameSite flags on a cookie in PHP?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookCookie Flags: Secure, HttpOnly, and SameSite

Свайпніть щоб показати меню

When you work with cookies in PHP, understanding cookie security flags is essential to protect your users. Three key flags—Secure, HttpOnly, and SameSite—play a vital role in defending against common web vulnerabilities. Each flag controls how and when a cookie is sent or accessed, reducing the risk of sensitive data exposure.

set_secure_cookie.php

set_secure_cookie.php

copy
1234567891011121314151617
<?php // Set a cookie with Secure, HttpOnly, and SameSite flags setcookie( "user_token", "abc123secure", [ "expires" => time() + 3600, // 1 hour from now "path" => "/", "domain" => "", // current domain "secure" => true, // only send over HTTPS "httponly" => true, // not accessible via JavaScript "samesite" => "Strict" // only sent for same-site requests ] ); echo "Secure cookie set with Secure, HttpOnly, and SameSite=Strict flags."; ?>

In the code above, you set a cookie named user_token with all three security flags enabled. The Secure flag ensures the cookie is only sent over HTTPS connections, protecting it from being intercepted on unencrypted networks. The HttpOnly flag prevents JavaScript on the page from accessing the cookie, blocking many cross-site scripting (XSS) attacks. The SameSite flag, set to "Strict", tells browsers to only send the cookie for requests originating from the same site, reducing the risk of cross-site request forgery (CSRF).

Each flag contributes to a layered defense:

  • Secure: only transmits the cookie over encrypted HTTPS connections;
  • HttpOnly: hides the cookie from client-side scripts;
  • SameSite: restricts when the browser sends the cookie, controlling cross-site transmission.

If you omit these flags, your cookies may be exposed to various attacks. Consider what happens when you set a cookie without these protections.

set_insecure_cookie.php

set_insecure_cookie.php

copy
123456
<?php // Set a cookie without Secure, HttpOnly, or SameSite flags setcookie("user_token", "abc123insecure", time() + 3600); echo "Insecure cookie set without Secure, HttpOnly, or SameSite flags."; ?>

Comparing the two approaches, cookies set with all three flags are much better protected. The first example ensures cookies are only sent over secure channels, hidden from scripts, and limited to same-site requests. The second example leaves cookies exposed to interception, script access, and cross-site attacks. Always use these flags for any cookie that stores sensitive or authentication-related data.

question mark

Which of the following statements best describes the combined effect of setting Secure, HttpOnly, and SameSite flags on a cookie in PHP?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 2
some-alt