Nothing like a minimal and reduced example. Looking at the documentation of php has two main problems that may happen. Something be printed before the call to setcookie()
, causing the call to function to return false
. Or it may be that the cookie created is limited to a specific subdirectory (the fourth parameter of the function setcookie()
serves to indicate in which directories the cookie will be visible). Starting from these two problems create this folder structure to exemplify:
/(raiz)
index.php
DiretorioA
a.php
DiretorioB
b.php
The archive index php. has the following content:
<?php
//Diretorio raiz '/index.php'
if(!isset($_COOKIE['cookie_na_raiz'])){
$status = setcookie("cookie_na_raiz",
'Este cookie foi criado na raiz do site', time()+3600);
if($status){
echo 'Cookie criado com sucesso';
}else{
echo 'Erro ao criar cookie';
}
}
var_dump($_COOKIE);
Content of a. php:
<?php
//Diretorio a '/DiretorioA/a.php'
if(!isset($_COOKIE['cookie_na_pasta_a'])){
$status = setcookie("cookie_na_pasta_a",
'Este cookie foi criado na pasta a', time()+3600, '/');
if($status){
echo 'Cookie criado com sucesso';
}else{
echo 'Erro ao criar cookie';
}
}
var_dump($_COOKIE);
And finally content of b. php:
<?php
//Diretorio b '/DiretorioB/b.php'
if(!isset($_COOKIE['cookie_na_pasta_b'])){
$status = setcookie("cookie_na_pasta_b",
'Este cookie foi criado na pasta b', time()+3600);
if($status){
echo 'Cookie criado com sucesso';
}else{
echo 'Erro ao criar cookie';
}
}
var_dump($_COOKIE);
Run each of the files (no matter the sequence) just to create cookies.
When you access the file index php. (after following the above step) will be printed:
'cookie_na_raiz' => string 'Este cookie foi criado na raiz do site' (length=38)
'cookie_na_pasta_a' => string 'Este cookie foi criado na pasta a' (length=33)
When accessing the.php file for the second time it will be printed:
'cookie_na_raiz' => string 'Este cookie foi criado na raiz do site' (length=38)
'cookie_na_pasta_a' => string 'Este cookie foi criado na pasta a' (length=33)
And finally, when accessing the b.php file will be printed:
'cookie_na_raiz' => string 'Este cookie foi criado na raiz do site' (length=38)
'cookie_na_pasta_a' => string 'Este cookie foi criado na pasta a' (length=33)
'cookie_na_pasta_b' => string 'Este cookie foi criado na pasta b' (length=33)
This shows the difference in visibility according to the domain in which the cookie was created. Reproduce this example and one of the two situations must be happening in your code.
I think you need to refresh the page to appear, the cookie will be created after the next upload;
– Sveen
I try to use the cookie on another page and it doesn’t work either, I just don’t think it’s creating the cookie
– Music Lyrics HQ
to see if it was created in "Inspect Element > Storage > Cookies", if it does not find it may be that your browser or the S.O. is blocking
– Sveen
I usually use only setcookie("data", serialize($data), time() + 3600 * 24 * 365);
– Sveen
For me your code is correct, if $cookie_emailu and $cookie_email is a string
– Sveen
because not even using $_SESSION works, I want to move things to another page and I can’t
– Music Lyrics HQ
If you’re going to use $_SESSION don’t forget session_start(); first of all.
– Sveen
I know, I’m starting to think it’s a server problem because this is okay and I’m stuck in a beginner’s problem
– Music Lyrics HQ
In case you don’t need to use $_SESSION to move to another page, as you set the Cookie it will be saved in the browser until its time expires or you remove it
– Sveen
I know the problem is that it doesn’t get stored in the cookie
– Music Lyrics HQ