0Pricing
PHP Academy · Lesson

Setting and Reading Cookies

Create cookies with setcookie and read them from _COOKIE.

Setting and Reading Cookies is a free PHP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Cookies?

Cookies are small pieces of data stored in the browser. The server sends them via the Set-Cookie header; the browser sends them back on every subsequent request.

setcookie()

setcookie() must be called before any output. It adds a Set-Cookie header to the response.

<?php
setcookie("username", "alice", time() + 3600);

setcookie() Parameters

Full signature: setcookie(name, value, expires, path, domain, secure, httponly).

<?php
setcookie(
    "prefs",
    "dark-mode",
    time() + 86400,
    "/",
    "",
    true,
    true
);

Reading a Cookie

Access cookies from $_COOKIE on the next request (cookies set in the current request are not yet in $_COOKIE).

<?php
if (isset($_COOKIE["username"])) {
    echo "Hello, ".$_COOKIE["username"];
}

Deleting a Cookie

Set the cookie with an expiry in the past. The browser will delete it.

<?php
setcookie("username", "", time() - 3600, "/");

Cookie Expiry

Pass a Unix timestamp for $expires. Use time() + seconds. Pass 0 for a session cookie that expires when the browser closes.

Cookie Path and Domain

$path restricts which URL paths see the cookie. / means all paths. $domain allows sharing across subdomains: .example.com.

Cookie Size Limit

Browsers limit each cookie to ~4 KB. For larger data, store it server-side and use only a reference ID in the cookie.

Cookie Arrays

PHP supports array-style cookies using bracket notation in the name.

<?php
setcookie("filters[color]", "blue");
setcookie("filters[size]", "XL");
// Read: $_COOKIE["filters"]["color"]

Inspecting Cookies in DevTools

Open DevTools → Application → Cookies to view all cookies. Check Secure, HttpOnly, and SameSite flags here.

Cookies vs Sessions

Cookies: client-side, limited size, accessible by JS (unless HttpOnly), persistent. Sessions: server-side, unlimited size, more secure.

Summary

Use setcookie() before output. Read from $_COOKIE on the next request. Set expiry, path, domain, Secure, and HttpOnly appropriately.

Quick Check

When is a cookie set with setcookie() available in $_COOKIE?

Frequently asked questions

Is the “Setting and Reading Cookies” lesson free?

Yes — the full text of “Setting and Reading Cookies” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Setting and Reading Cookies”?

Create cookies with setcookie and read them from _COOKIE. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Setting and Reading Cookies” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Starting and Using Sessions
  2. Session Security Best Practices
  3. Setting and Reading Cookies
  4. Cookie Security: HttpOnly and Secure Flags
← Back to PHP Academy