| Lesson 5 | Creating cookies |
| Objective | Describe how cookies are created via the browser when a user visits a website |
When a user visits a website, the web browser and web server engage in a structured HTTP exchange. Cookies are created as part of this exchange through HTTP response headers sent from the server to the browser. Understanding this mechanism is essential for modern e-commerce platforms, authentication systems, personalization engines, analytics dashboards, and composable commerce architectures.
Cookies are not “written” by the browser arbitrarily. They are created when a server includes a Set-Cookie header in its HTTP response.
The browser receives the header, validates its attributes, and stores the cookie according to defined scope rules (domain, path, expiration, security flags). On subsequent requests to matching URLs, the browser automatically includes the stored cookie in the Cookie request header.
A simplified example of a cookie header appears below:
Set-Cookie: NAME=VALUE; expires=DATE;
path=PATH; domain=DOMAIN_NAME; Secure; HttpOnly; SameSite=Lax
Modern browsers enforce strict rules. If the attributes violate current security policies—such as SameSite=None without Secure—the cookie will be rejected.
Set-Cookie header.In legacy environments, CGI scripts handled this. Today, modern frameworks—Laravel, Express.js, Spring Boot, Rails—abstract this process behind response objects.
Set-Cookie: NAME=VALUE; expires=DATE;
path=PATH; domain=DOMAIN_NAME; secure
Unlike early web eras, browsers now default to SameSite=Lax.
Third-party cookies are increasingly blocked.
Chrome, Safari, and Firefox implement partitioned storage models to mitigate tracking abuse.
Best practice in 2026:
Set-Cookie: sessionId=abc123;
Secure;
HttpOnly;
SameSite=Lax;
Path=/;
Domain=example.com
Shopify automatically manages storefront session cookies for cart state, checkout flow, and authentication. Merchants interact with data via Shopify Admin and Shopify Analytics, not direct cookie manipulation.
WooCommerce relies on WordPress sessions and cookies to manage cart contents. WooCommerce Analytics uses first-party cookies to measure conversions.
BigCommerce uses cookies for storefront sessions while exposing data through APIs and the BigCommerce Control Panel.
Magento 2 modernized its cookie handling compared to Magento 1.x, adding improved SameSite support and enhanced session isolation.
Legacy platforms such as osCommerce, Zen Cart, and Magento 1.x often relied heavily on session cookies without strict security flags.
Modern commerce models include:
In headless systems, cookies may authenticate API calls, store JWT refresh tokens, or manage localized cart state.
Traditional merchant accounts processed payments within hosted checkout pages. Modern gateways like Stripe, PayPal, and Square integrate via secure redirect flows or embedded components.
Stripe Dashboard, PayPal Developer Tools, and Square Console allow administrators to monitor session flows. Cookies often preserve checkout state during redirection between domains.
Headless CMS platforms like Contentful and Sanity deliver structured content through APIs. When combined with modern CDNs such as Cloudflare or Fastly, cookie-based personalization may occur at the edge.
Edge computing solutions can inspect cookies to determine geo-location, A/B test variants, or language preferences before serving content.
Use browser DevTools:
document.cookie.Modern browsers no longer store cookies in simple text files. Chrome stores them in a SQLite database within the browser profile directory. Safari and Firefox use structured storage models. Manual editing is discouraged because corruption may invalidate session integrity.
Cookies are only accepted when delivered via HTTP(S) responses.
Chrome intentionally blocks cookies from file:/// protocol contexts.
Cookies are created through a deliberate, standards-based HTTP mechanism. They enable session continuity, personalization, cart management, authentication, analytics, and state persistence across modern web applications.
From legacy CGI scripts to headless commerce stacks powered by Next.js and Stripe APIs, the browser remains the controlled execution environment that enforces cookie policy. Modern security defaults—Secure, HttpOnly, SameSite—protect user privacy while enabling scalable digital commerce.
Understanding how cookies are created via the browser provides foundational knowledge for designing secure e-commerce systems, implementing composable architectures, and troubleshooting real-world deployment environments.