Master da Web
Back to Blog

PHP: How to Create and Read Cookies

LucasMarch 4, 20121 min readUpdated on November 4, 2023

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

 

// 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.

 

// 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.

 

Tags#cookie#PHP
Share

Need Hosting or Servers?

Discover our VPS, dedicated servers and professional hosting solutions

Related Articles

View all articles