Funzione PHP setrawcookie()
Esempio
L'esempio seguente crea un cookie con PHP. Il cookie è denominato "utente" e il valore sarà "John Doe". Il valore del cookie non sarà codificato nell'URL. Il cookie scadrà dopo 30 giorni (86400 * 30). L'utilizzo di "/", significa che il cookie è disponibile nell'intero sito web (in caso contrario, seleziona la directory che preferisci):
<?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>
?>
Definizione e utilizzo
La funzione setrawcookie() definisce un cookie (senza codifica URL) da inviare insieme al resto delle intestazioni HTTP.
Un cookie viene spesso utilizzato per identificare un utente. Un cookie è un piccolo file che il server incorpora nel computer dell'utente. Ogni volta che lo stesso computer richiede una pagina con un browser, invierà anche il cookie. Con PHP, puoi creare e recuperare i valori dei cookie.
Il nome del cookie viene automaticamente assegnato ad una variabile con lo stesso nome. Ad esempio, se è stato inviato un cookie con il nome "utente", viene automaticamente creata una variabile denominata $utente, contenente il valore del cookie.
Nota: la funzione setrawcookie() deve apparire PRIMA del tag <html>.
Nota: per codificare automaticamente in URL il valore del cookie durante l'invio e per decodificarlo automaticamente durante la ricezione, utilizzare invece la funzione setcookie() .
Sintassi
setrawcookie(name, value, expire, path, domain, secure);
Valori dei parametri
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. |
Dettagli tecnici
Valore di ritorno: | VERO sul successo. FALSO in caso di fallimento |
---|---|
Versione PHP: | 5+ |
Altri esempi
Esempio
Recupera il valore del cookie denominato "utente" (usando la variabile globale $_COOKIE). Usa anche la funzione isset() per scoprire se il cookie esiste:
<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>
Esempio
Per modificare un cookie è sufficiente impostare (di nuovo) il cookie utilizzando la funzione 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>
Esempio
Per eliminare un cookie, utilizzare la funzione setrawcookie() con una data di scadenza nel passato:
<?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>
Esempio
Crea un piccolo script che controlla se i cookie sono abilitati. Per prima cosa, prova a creare un cookie di prova con la funzione setrawcookie(), quindi conta la variabile array $_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>
❮ Riferimento alla rete PHP