PHP: How to Create and Read Cookies

shape
shape
shape
shape
shape
shape
shape
shape

Cookies are data exchanged between the browser and the server, used to store user preferences, login and so on.

 

How to create a Cookie in PHP?

// Criando Cookies
$nome = "masterdaweb";
$valor = "Cookie Criado com Sucesso";
$expira = time() + 3600;
setcookie($nome, $valor, $expira);

$name – Cookie name.

$value – The value that will be stored in the Cookie.

$expires – When the cookie expires. Note that the value is given in seconds, 3600 seconds corresponds to 1 hour.

setcookie() – Function used to create the Cookie from the data obtained in the variables above.

 

How to read a Cookie in PHP?

// Lê o cookie "masterdaweb"
$ler = $_COOKIE['masterdaweb'];

echo "Valor do Cookie masterdaweb: $ler";

$_COOKIE – Superglobal variable, used to read cookies. Just use the variable with the name of the Cookie you created, as in the example above.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest news

Latest news directly from our blog.