$_COOKIE
$_COOKIE: array<string,string> — associative array parsed from the Cookie: request header (keys are cookie names)
Reads request cookies: under Web SAPI the Cookie request header is parsed automatically into the $_COOKIE array.
C C23
#include <stdio.h>
#include <string.h>
int main(void) {
/* CGI convention: the Cookie request header comes from getenv("HTTP_COOKIE"); simulated with a fixed string on CLI */
const char *raw = "theme=dark; lang=zh-CN; sessionid=abc123";
char keys[8][32], vals[8][64];
int n = 0;
const char *p = raw;
while (*p) {
const char *semi = strchr(p, ';');
int seg = semi ? (int)(semi - p) : (int)strlen(p);
const char *eq = memchr(p, '=', seg);
if (eq) {
int klen = (int)(eq - p);
memcpy(keys[n], p, klen); keys[n][klen] = '\0';
int vlen = seg - klen - 1;
memcpy(vals[n], eq + 1, vlen); vals[n][vlen] = '\0';
n++;
}
if (!semi) break;
p = semi + 2; /* Skip "; " */
}
const char *theme = NULL, *lang = NULL;
for (int i = 0; i < n; i++) {
if (strcmp(keys[i], "theme") == 0) theme = vals[i];
if (strcmp(keys[i], "lang") == 0) lang = vals[i];
}
printf("theme: %s\n", theme ? theme : "NULL");
printf("lang: %s\n", lang ? lang : "NULL");
printf("count: %d\n", n);
int found = 0;
for (int i = 0; i < n; i++) {
if (strcmp(keys[i], "missing") == 0) found = 1;
}
printf("missing: %s\n", found ? "yes" : "no");
return 0;
}
theme: dark lang: zh-CN count: 3 missing: no
C++ C++26
#include <iostream>
#include <map>
#include <string>
int main() {
// CGI convention: getenv("HTTP_COOKIE") fetches the Cookie header; hand-written parsing
std::string raw = "theme=dark; lang=zh-CN; sessionid=abc123";
std::map<std::string, std::string> jar;
size_t pos = 0;
while (pos < raw.size()) {
size_t semi = raw.find("; ", pos);
size_t seg = (semi == std::string::npos ? raw.size() : semi) - pos;
size_t eq = raw.find('=', pos);
if (eq != std::string::npos && eq < pos + seg) {
jar[raw.substr(pos, eq - pos)] = raw.substr(eq + 1, seg - (eq - pos) - 1);
}
pos = (semi == std::string::npos) ? raw.size() : semi + 2;
}
auto get = [&](const std::string &k) {
auto it = jar.find(k);
return it == jar.end() ? std::string("NULL") : it->second;
};
std::cout << "theme: " << get("theme") << "\n";
std::cout << "lang: " << get("lang") << "\n";
std::cout << "count: " << jar.size() << "\n";
std::cout << "missing: " << (jar.count("missing") ? "yes" : "no") << "\n";
return 0;
}
theme: dark lang: zh-CN count: 3 missing: no
C# v11.0.100
// ASP.NET's IRequestCookieCollection lives at the framework level; the console parses the Cookie request header by hand
var raw = "theme=dark; lang=zh-CN; sessionid=abc123";
var jar = new Dictionary<string, string>();
foreach (var pair in raw.Split("; "))
{
int eq = pair.IndexOf('=');
jar[pair[..eq]] = pair[(eq + 1)..];
}
Console.WriteLine($"theme: {jar["theme"]}");
Console.WriteLine($"lang: {jar["lang"]}");
Console.WriteLine($"count: {jar.Count}");
Console.WriteLine($"missing: {(jar.ContainsKey("missing") ? "yes" : "no")}");
theme: dark lang: zh-CN count: 3 missing: no
Go v1.27.4
package main
import (
"bufio"
"fmt"
"net/http"
"strings"
)
func main() {
// Standard library net/http: build the request with http.ReadRequest, then req.Cookie(name) fetches directly (Cookies() gets all)
reqText := "GET / HTTP/1.0\r\nCookie: theme=dark; lang=zh-CN; sessionid=abc123\r\n\r\n"
req, _ := http.ReadRequest(bufio.NewReader(strings.NewReader(reqText)))
if c, err := req.Cookie("theme"); err == nil {
fmt.Println("theme:", c.Value)
}
if c, err := req.Cookie("lang"); err == nil {
fmt.Println("lang:", c.Value)
}
fmt.Println("count:", len(req.Cookies()))
if _, err := req.Cookie("missing"); err != nil {
fmt.Println("missing: no")
} else {
fmt.Println("missing: yes")
}
}
theme: dark lang: zh-CN count: 3 missing: no
Java v27.0.1
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// Servlet's request.getCookies() lives at the framework level; the standard library parses the Cookie request header by hand
String raw = "theme=dark; lang=zh-CN; sessionid=abc123";
Map<String, String> jar = new HashMap<>();
for (String pair : raw.split("; ")) {
int eq = pair.indexOf('=');
jar.put(pair.substring(0, eq), pair.substring(eq + 1));
}
System.out.println("theme: " + jar.get("theme"));
System.out.println("lang: " + jar.get("lang"));
System.out.println("count: " + jar.size());
System.out.println("missing: " + (jar.containsKey("missing") ? "yes" : "no"));
}
}
theme: dark lang: zh-CN count: 3 missing: no
JavaScript v26.13.1
// Node has no built-in Cookie parsing (the framework cookie-parser is the real equivalent); hand-parse the Cookie request header
const raw = "theme=dark; lang=zh-CN; sessionid=abc123";
const jar = new Map();
for (const pair of raw.split("; ")) {
const eq = pair.indexOf("=");
jar.set(pair.slice(0, eq), pair.slice(eq + 1));
}
console.log(`theme: ${jar.get("theme")}`);
console.log(`lang: ${jar.get("lang")}`);
console.log(`count: ${jar.size}`);
console.log(`missing: ${jar.has("missing") ? "yes" : "no"}`);
theme: dark lang: zh-CN count: 3 missing: no
PHP v8.6.0
// $_COOKIE: under a Web SAPI it is parsed and filled from the Cookie: request header (key-value pairs, auto-urldecoded)
// On CLI, simulate one parse by hand — same logic as PHP's internals: split on semicolons, split keys from values on =
$raw = "theme=dark; lang=zh-CN; sessionid=abc123";
$_COOKIE = [];
foreach (explode("; ", $raw) as $pair) {
[$k, $v] = explode("=", $pair, 2);
$_COOKIE[$k] = $v;
}
echo "theme: ", $_COOKIE["theme"], "\n";
echo "lang: ", $_COOKIE["lang"], "\n";
echo "count: ", count($_COOKIE), "\n";
echo "missing: ", isset($_COOKIE["missing"]) ? "yes" : "no";
theme: dark lang: zh-CN count: 3 missing: no
Python v3.15.2
from http.cookies import SimpleCookie
# Standard library http.cookies: SimpleCookie.load parses the Cookie request header (web frameworks do it for you)
raw = "theme=dark; lang=zh-CN; sessionid=abc123"
jar = SimpleCookie()
jar.load(raw)
print("theme:", jar["theme"].value)
print("lang:", jar["lang"].value)
print("count:", len(jar))
print("missing:", "no" if "missing" not in jar else "yes")
theme: dark lang: zh-CN count: 3 missing: no
Rust v1.99.1
use std::collections::HashMap;
fn main() {
// No Cookie parsing in the standard library (real projects use the cookie crate or framework extractors); hand-written parsing
let raw = "theme=dark; lang=zh-CN; sessionid=abc123";
let mut jar: HashMap<&str, &str> = HashMap::new();
for pair in raw.split("; ") {
let (k, v) = pair.split_once('=').unwrap();
jar.insert(k, v);
}
println!("theme: {}", jar["theme"]);
println!("lang: {}", jar["lang"]);
println!("count: {}", jar.len());
println!("missing: {}", if jar.contains_key("missing") { "yes" } else { "no" });
}
theme: dark lang: zh-CN count: 3 missing: no
TypeScript v26.13.1
// Node has no built-in Cookie parsing (the framework cookie-parser is the real equivalent); hand-parse the Cookie request header
const raw = "theme=dark; lang=zh-CN; sessionid=abc123";
const jar = new Map<string, string>();
for (const pair of raw.split("; ")) {
const eq = pair.indexOf("=");
jar.set(pair.slice(0, eq), pair.slice(eq + 1));
}
console.log(`theme: ${jar.get("theme")}`);
console.log(`lang: ${jar.get("lang")}`);
console.log(`count: ${jar.size}`);
console.log(`missing: ${jar.has("missing") ? "yes" : "no"}`);
theme: dark lang: zh-CN count: 3 missing: no
Language differences
C: the CGI idiom getenv("HTTP_COOKIE") fetches the raw header; hand-parse by semicolon/equals; no map, so keys are found linearly.
C++: same as C; std::map holds the pairs.
C#: the framework layer is IRequestCookieCollection (ASP.NET); consoles hand-parse into a Dictionary.
Go: the standard library has it — req.Cookie(name) (http.ErrNoCookie when missing) and req.Cookies(); no hand parsing server-side.
Java: Servlet request.getCookies() is framework-layer (returns a Cookie array); the standard library path hand-splits.
JavaScript: nothing built into Node (cookie-parser middleware is the read-side idiom); split("; ") plus equals covers simple cases — remember decodeURIComponent for real ones.
PHP: $_COOKIE is a superglobal array — Web SAPI parses the Cookie request header (with urldecode) automatically and you read it like any array; missing keys use isset; on CLI $_COOKIE stays empty (the example simulates parsing by hand).
Python: the standard library's http.cookies.SimpleCookie.load parses the header (values via Morsel); web frameworks expose it pre-parsed as request.cookies.
Rust: nothing in std — the cookie crate's Cookie::parse or a framework extractor.
TypeScript: same as JavaScript.