PHP setcookie() Funktion
Beispiel
Das folgende Beispiel erstellt ein Cookie namens „user“ mit dem Wert „John Doe“. Das Cookie läuft nach 30 Tagen ab (86400 * 30). Das „/“ bedeutet, dass das Cookie auf der gesamten Website verfügbar ist (ansonsten wählen Sie das gewünschte Verzeichnis aus).
Wir rufen dann den Wert des Cookies „Benutzer“ ab (unter Verwendung der globalen Variable $_COOKIE). Wir verwenden auch die Funktion isset(), um herauszufinden, ob das Cookie gesetzt wird:
<!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Definition und Verwendung
Die Funktion setcookie() definiert ein Cookie, das zusammen mit den restlichen HTTP-Headern gesendet werden soll.
Ein Cookie wird häufig verwendet, um einen Benutzer zu identifizieren. Ein Cookie ist eine kleine Datei, die der Server auf dem Computer des Benutzers einbettet. Jedes Mal, wenn derselbe Computer eine Seite mit einem Browser anfordert, sendet er auch das Cookie. Mit PHP können Sie Cookie-Werte sowohl erstellen als auch abrufen.
Der Name des Cookies wird automatisch einer gleichnamigen Variablen zugeordnet. Wenn beispielsweise ein Cookie mit dem Namen „user“ gesendet wurde, wird automatisch eine Variable namens „$user“ erstellt, die den Cookie-Wert enthält.
Hinweis: Die setcookie()-Funktion muss VOR dem <html>-Tag erscheinen.
Hinweis: Der Wert des Cookies wird beim Senden des Cookies automatisch URL-codiert und beim Empfang automatisch decodiert (um URL-Codierung zu verhindern, verwenden Sie stattdessen setrawcookie() ).
Syntax
setcookie(name, value, expire, path, domain, secure, httponly);
Parameterwerte
Parameter | Description |
---|---|
name | Required. Specifies the name of the cookie |
value | Optional. Specifies the value of the cookie |
expire | Optional. Specifies when the cookie expires. The value: time()+86400*30, will set the cookie to expire in 30 days. If this parameter is omitted or set to 0, the cookie will expire at the end of the session (when the browser closes). Default is 0 |
path | Optional. Specifies the server path of the cookie. If set to "/", the cookie will be available within the entire domain. If set to "/php/", the cookie will only be available within the php directory and all sub-directories of php. The default value is the current directory that the cookie is being set in |
domain | Optional. Specifies the domain name of the cookie. To make the cookie available on all subdomains of example.com, set domain to "example.com". Setting it to www.example.com will make the cookie only available in the www subdomain |
secure | Optional. Specifies whether or not the cookie should only be transmitted over a secure HTTPS connection. TRUE indicates that the cookie will only be set if a secure connection exists. Default is FALSE |
httponly | Optional. If set to TRUE the cookie will be accessible only through the HTTP protocol (the cookie will not be accessible by scripting languages). This setting can help to reduce identity theft through XSS attacks. Default is FALSE |
Technische Details
Rückgabewert: | TRUE auf Erfolg. FALSE bei Fehler |
---|---|
PHP-Version: | 4+ |
PHP-Änderungsprotokoll: | PHP 5.5 - Ein Max-Age-Attribut wurde in den Set-Cookie-Header aufgenommen, der an den Client gesendet wird. PHP 5.2 - Der httponly-Parameter wurde hinzugefügt |
Mehr Beispiele
Beispiel
Mehrere Ablaufdaten für Cookies:
<?php
$value = "Hello world!";
// cookie will expire when the browser close
setcookie("myCookie", $value);
// cookie will expire in 1 hour
setcookie("myCookie", $value, time() + 3600);
// cookie will expire in 1 hour, and will only be available
// within the php directory + all sub-directories of php
setcookie("myCookie", $value, time() + 3600, "/php/");
?>
<html>
<body>
...some code...
</body>
</html>
Beispiel
Um ein Cookie zu ändern, setzen Sie das Cookie einfach (erneut) mit der Funktion setcookie():
<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Beispiel
Verwenden Sie zum Löschen eines Cookies die Funktion setcookie() mit einem Ablaufdatum in der Vergangenheit:
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
Beispiel
Erstellen Sie ein kleines Skript, das überprüft, ob Cookies aktiviert sind. Versuchen Sie zuerst, ein Test-Cookie mit der Funktion setcookie() zu erstellen, und zählen Sie dann die Array-Variable $_COOKIE:
<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>
</body>
</html>
❮ PHP-Netzwerkreferenz