PHP setrawcookie() Funktion
Beispiel
Das folgende Beispiel erstellt ein Cookie mit PHP. Das Cookie heißt „user“ und der Wert ist „John Doe“. Der Cookie-Wert wird nicht URL-kodiert. Das Cookie läuft nach 30 Tagen ab (86400 * 30). Die Verwendung von "/" bedeutet, dass das Cookie auf der gesamten Website verfügbar ist (andernfalls wählen Sie das gewünschte Verzeichnis aus):
<?php
$cookie_name = "user";
$cookie_value = "John";
setrawcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
// 86400 = 1 day
?>
<html>
<body>
<?php
echo "Cookie is set.";
?>
</body>
</html>
?>
Definition und Verwendung
Die Funktion setrawcookie() definiert ein Cookie (ohne URL-Codierung), das zusammen mit den restlichen HTTP-Headern gesendet wird.
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 setrawcookie()-Funktion muss VOR dem <html>-Tag erscheinen.
Hinweis: Um den Cookie-Wert beim Senden automatisch per URL zu codieren und beim Empfang automatisch zu decodieren, verwenden Sie stattdessen die Funktion setcookie() .
Syntax
setrawcookie(name, value, expire, path, domain, secure);
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 not set, the cookie will expire at the end of the session (when the browser closes) |
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. |
Technische Details
Rückgabewert: | TRUE auf Erfolg. FALSE bei Fehler |
---|---|
PHP-Version: | 5+ |
Mehr Beispiele
Beispiel
Rufen Sie den Wert des Cookies mit dem Namen "Benutzer" ab (unter Verwendung der globalen Variable $_COOKIE). Verwenden Sie auch die Funktion isset(), um herauszufinden, ob das Cookie existiert:
<html>
<body>
<?php
$cookie_name = "user";
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' does not exist!";
} else {
echo "Cookie is named: " . $cookie_name . "<br>Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Beispiel
Um ein Cookie zu ändern, setzen Sie das Cookie einfach (erneut) mit der Funktion setrawcookie():
<?php
$cookie_name = "user";
$cookie_value = "Alex";
setrawcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<?php
$cookie_name = "user";
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' does not exist!";
} else {
echo "Cookie is named: " . $cookie_name . "<br>Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
Beispiel
Verwenden Sie zum Löschen eines Cookies die Funktion setrawcookie() mit einem Ablaufdatum in der Vergangenheit:
<?php
$cookie_name = "user";
unset($_COOKIE[$cookie_name]);
// empty value and expiration one hour before
$res = setrawcookie($cookie_name, '', 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 zunächst, ein Test-Cookie mit der Funktion setrawcookie() zu erstellen, und zählen Sie dann die Array-Variable $_COOKIE:
<?php
setrawcookie("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