Funkce PHP setcookie().
Příklad
Následující příklad vytvoří soubor cookie s názvem „user“ s hodnotou „John Doe“. Platnost cookie vyprší po 30 dnech (86400 * 30). „/“ znamená, že soubor cookie je dostupný na celé webové stránce (v opačném případě vyberte adresář, který preferujete).
Poté načteme hodnotu cookie „user“ (pomocí globální proměnné $_COOKIE). Také používáme funkci isset() ke zjištění, zda je soubor cookie nastaven:
<!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>
Definice a použití
Funkce setcookie() definuje cookie, která má být odeslána spolu se zbytkem HTTP hlaviček.
K identifikaci uživatele se často používá cookie. Cookie je malý soubor, který server vloží do počítače uživatele. Pokaždé, když stejný počítač požádá o stránku s prohlížečem, odešle také cookie. Pomocí PHP můžete vytvářet i získávat hodnoty souborů cookie.
Název souboru cookie je automaticky přiřazen ke stejnojmenné proměnné. Pokud byl například soubor cookie odeslán se jménem „user“, automaticky se vytvoří proměnná s názvem $user obsahující hodnotu cookie.
Poznámka: Funkce setcookie() se musí objevit PŘED tagem <html>.
Poznámka: Hodnota cookie je automaticky URLencoded při odesílání cookie a automaticky dekódována, když je přijata (abyste zabránili URLencookingu, použijte místo toho setrawcookie() ).
Syntax
setcookie(name, value, expire, path, domain, secure, httponly);
Hodnoty parametrů
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 |
Technické údaje
Návratová hodnota: | PRAVDA o úspěchu. NEPRAVDA při selhání |
---|---|
Verze PHP: | 4+ |
PHP Changelog: | PHP 5.5 – Atribut Max-Age byl zahrnut do hlavičky Set-Cookie odeslané klientovi PHP 5.2 – Byl přidán parametr httponly |
Další příklady
Příklad
Několik dat vypršení platnosti souborů cookie:
<?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>
Příklad
Chcete-li upravit cookie, stačí nastavit (znovu) cookie pomocí funkce 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>
Příklad
Chcete-li soubor cookie odstranit, použijte funkci setcookie() s datem vypršení platnosti v minulosti:
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
Příklad
Vytvořte malý skript, který kontroluje, zda jsou povoleny soubory cookie. Nejprve zkuste vytvořit testovací cookie pomocí funkce setcookie() a poté spočítejte proměnnou pole $_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>
❮ Síťová reference PHP