setcookie

setcookie(string $name, string $value = "", array $expires = 0, string $path = "", string $domain = "", bool $secure = false, bool $httponly = false): bool

Queues a Set-Cookie response header (expiry/path/domain/HttpOnly/Secure are all header parameters); must be called before any output.

C C23

C23
#include <stdio.h>

int main(void) {
    /* CGI/hand-rolled HTTP server: hand-write the Set-Cookie header (what PHP's setcookie does for you) */
    printf("set: Set-Cookie: theme=dark; Path=/; HttpOnly; Secure\n");
    /* Deleting a cookie = Max-Age=0 */
    printf("del: Set-Cookie: tracker=; Path=/; Max-Age=0\n");
    return 0;
}
Output
set: Set-Cookie: theme=dark; Path=/; HttpOnly; Secure
del: Set-Cookie: tracker=; Path=/; Max-Age=0

C++ C++26

C++26
#include <iostream>

int main() {
    // CGI/hand-rolled HTTP server: hand-write the Set-Cookie header
    std::cout << "set: Set-Cookie: theme=dark; Path=/; HttpOnly; Secure\n";
    // Deleting a cookie = Max-Age=0
    std::cout << "del: Set-Cookie: tracker=; Path=/; Max-Age=0\n";
    return 0;
}
Output
set: Set-Cookie: theme=dark; Path=/; HttpOnly; Secure
del: Set-Cookie: tracker=; Path=/; Max-Age=0

C# v11.0.100

v11.0.100
// ASP.NET's Cookie API lives at the framework level (IResponseCookies); the console hand-writes the Set-Cookie header string
Console.WriteLine("set: Set-Cookie: theme=dark; Path=/; HttpOnly; Secure");
// Deleting a cookie = Max-Age=0
Console.WriteLine("del: Set-Cookie: tracker=; Path=/; Max-Age=0");
Output
set: Set-Cookie: theme=dark; Path=/; HttpOnly; Secure
del: Set-Cookie: tracker=; Path=/; Max-Age=0

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
)

func main() {
	// Standard library net/http: http.SetCookie assembles and writes the Set-Cookie header (demonstrated with an httptest recorder)
	rec := httptest.NewRecorder()
	http.SetCookie(rec, &http.Cookie{
		Name: "theme", Value: "dark", Path: "/", HttpOnly: true, Secure: true,
	})
	fmt.Println("set: Set-Cookie:", rec.Header().Get("Set-Cookie"))
	// Deleting a cookie: a negative MaxAge means "expire now" (emitted as Max-Age=0)
	rec2 := httptest.NewRecorder()
	http.SetCookie(rec2, &http.Cookie{Name: "tracker", Path: "/", MaxAge: -1})
	fmt.Println("del: Set-Cookie:", rec2.Header().Get("Set-Cookie"))
}
Output
set: Set-Cookie: theme=dark; Path=/; HttpOnly; Secure
del: Set-Cookie: tracker=; Path=/; Max-Age=0

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        // Servlet's Cookie/response APIs are not in the standard library; hand-write the Set-Cookie header string (CGI/hand-rolled server scenario)
        // The customary attribute order: value → Path → HttpOnly → Secure
        System.out.println("set: Set-Cookie: theme=dark; Path=/; HttpOnly; Secure");
        // Deleting a cookie = Max-Age=0
        System.out.println("del: Set-Cookie: tracker=; Path=/; Max-Age=0");
    }
}
Output
set: Set-Cookie: theme=dark; Path=/; HttpOnly; Secure
del: Set-Cookie: tracker=; Path=/; Max-Age=0

JavaScript v26.13.1

v26.13.1
// Node has no built-in Cookie API (framework-level cookie/res.cookie is the real equivalent); hand-write the Set-Cookie header string
// The customary attribute order: value → Path → HttpOnly → Secure
const setLine = "Set-Cookie: theme=dark; Path=/; HttpOnly; Secure";
console.log("set:", setLine);
// Deleting a cookie = expiry placed in the past (Max-Age=0)
const delLine = "Set-Cookie: tracker=; Path=/; Max-Age=0";
console.log("del:", delLine);
Output
set: Set-Cookie: theme=dark; Path=/; HttpOnly; Secure
del: Set-Cookie: tracker=; Path=/; Max-Age=0

PHP v8.6.0

v8.6.0
// setcookie: registers the Set-Cookie response header; the options-array style is recommended since PHP 7.3
// Note: must be called before any output (returns false once headers are already sent)
$okSet = setcookie("theme", "dark", [
    "path" => "/",
    "httponly" => true,   // Not readable from JS (blocks XSS theft)
    "secure" => true,     // HTTPS-only transport
]);
// Deleting a cookie = expiry placed in the past (a Set-Cookie with expires in the past)
$okDel = setcookie("tracker", "", ["expires" => time() - 3600]);
echo "setcookie theme: ", var_export($okSet, true), "\n";
echo "setcookie tracker: ", var_export($okDel, true), "\n";
// The CLI SAPI never really sends headers: headers_list() stays empty (on the Web this would emit two Set-Cookie headers)
echo "headers in CLI: ", count(headers_list()), "\n";
// The reader's side: $_COOKIE is filled from the Cookie: request header (simulated once manually on CLI)
$_COOKIE["theme"] = "dark";
echo $_COOKIE["theme"], "\n";
Output
setcookie theme: true
setcookie tracker: true
headers in CLI: 0
dark

Python v3.15.2

v3.15.2
from http.cookies import SimpleCookie

# Hand-sent Set-Cookie header counterpart: the standard library http.cookies (writing the response header manually in a BaseHTTPServer scenario)
c = SimpleCookie()
c["theme"] = "dark"
c["theme"]["path"] = "/"
c["theme"]["httponly"] = True
c["theme"]["secure"] = True
print("set:", c.output())
# Deleting a cookie = expiry placed in the past (max-age=0)
d = SimpleCookie()
d["tracker"] = ""
d["tracker"]["path"] = "/"
d["tracker"]["max-age"] = 0
print("del:", d.output())
Output
set: Set-Cookie: theme=dark; HttpOnly; Path=/; Secure
del: Set-Cookie: tracker=""; Max-Age=0; Path=/

Rust v1.99.1

v1.99.1
fn main() {
    // No Cookie facilities in the standard library (cookie crate or framework extractors in real projects); hand-write the Set-Cookie header string
    println!("set: Set-Cookie: theme=dark; Path=/; HttpOnly; Secure");
    // Deleting a cookie = Max-Age=0
    println!("del: Set-Cookie: tracker=; Path=/; Max-Age=0");
}
Output
set: Set-Cookie: theme=dark; Path=/; HttpOnly; Secure
del: Set-Cookie: tracker=; Path=/; Max-Age=0

TypeScript v26.13.1

v26.13.1
// Node has no built-in Cookie API (framework-level cookie/res.cookie is the real equivalent); hand-write the Set-Cookie header string
const setLine: string = "Set-Cookie: theme=dark; Path=/; HttpOnly; Secure";
console.log(`set: ${setLine}`);
// Deleting a cookie = expiry placed in the past (Max-Age=0)
const delLine: string = "Set-Cookie: tracker=; Path=/; Max-Age=0";
console.log(`del: ${delLine}`);
Output
set: Set-Cookie: theme=dark; Path=/; HttpOnly; Secure
del: Set-Cookie: tracker=; Path=/; Max-Age=0

Language differences

C: hand-write the Set-Cookie: header at the CGI layer (printf into the header block), followed by \r\n\r\n before the body.

C++: same as C.

C#: the framework layer is ASP.NET's IResponseCookies.Append; the standard library's System.Net.Cookie is just a data structure — header serialization stays the framework's job; consoles hand-write the string.

Go: the standard library has it — http.SetCookie(w, &http.Cookie{...}) matches PHP semantics fully (Path/Domain/MaxAge/HttpOnly/Secure/SameSite); the deletion idiom is MaxAge: -1.

Java: the standard library's java.net.HttpCookie leans client-side parsing; server-side issuing is Servlet response.addCookie (framework layer); the example hand-writes the header.

JavaScript: nothing built into Node — hand-write the header string or use res.cookie (express); cookie-parser handles the read side.

PHP: setcookie is the one idiom, equivalent to assembling header("Set-Cookie: ..."); expires 0 means a session cookie (gone when the browser closes), a past time deletes; HttpOnly blocks XSS reads, Secure limits to HTTPS, SameSite fights CSRF (7.3+ options array); calling after any output returns false (headers sent).

Python: the standard library's http.cookies.SimpleCookie serializes Set-Cookie (mind its fixed attribute order: HttpOnly before Path); in web frameworks it's resp.set_cookie.

Rust: nothing in std — the cookie crate or a framework's cookie extractor/builder.

TypeScript: same as JavaScript.