Start · Sprachen · PHP · Referenz · setcookie

setcookie

Funktion

Setzt einen HTTP-Cookie. Seit PHP 7.3 als Options-Array — inkl. <code>SameSite</code>.

seit PHP 4.0.0 Kategorie: http

Signatur

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

Beschreibung

Für sicherheitskritische Cookies: ['secure' => true, 'httponly' => true, 'samesite' => 'Strict']. Muss VOR Body-Ausgabe geschehen.

Parameter

Name Typ Default Beschreibung
$name Pflicht string Cookie-Name.
$value string "" Wert.
$expires_or_options int|array 0 Ablaufzeit oder Options-Array.

Rückgabewert

Typ
bool
Beschreibung
true wenn ausgeliefert (Header noch nicht gesendet).

Beispiele

Sicherer Cookie

<?php

setcookie('session', $token, [
    'expires'  => time() + 3600,
    'path'     => '/',
    'domain'   => 'programmierung.net',
    'secure'   => true,
    'httponly' => true,
    'samesite' => 'Strict',
]);