It’s unclear where you’re invoking the isset()
. So the answer presupposes that it’s on the same page you invoked setcookie()
.
In this case it will not be accessible unless explicitly stated in the global variable $_COOKIE
.
setcookie("log", "true", time()+60, "/classes");
var_dump($_COOKIE); // Não retorna o índice "log", registrado acima em `setcookie()`
This is because the global variable $_COOKIE
with the index "log" will only be available in the next request.
To make it available on the same page where it is created, you must explicitly set so:
setcookie('log', true, time()+60, '/classes');
$_COOKIE['log'] = true;
Note: this behavior varies according to the browser and its version. Due to doubts, always seven explicitly the global variable.
To make it more convenient and reusable, create a routine:
function CookieSet($k, $v) {
setcookie($k, $v, time()+60, '/classes');
$_COOKIE[$k] = $v;
}
CookieSet('log', true);
Obviously, check if the fourth parameter of setcookie()
that is correct
Is defining how '/classes'
. You might want to use '/'
.
Defining as '/classes'
, will be limiting cookies to the directory '/classes'
. ex: http://localhost/classes
. Access http://localhost/
, will not be available.
First you’re checking to see if there is no! As soon as if it does not exist it will perform the function. Second if you set u "path" to the cookie should check it in this "path".
– Lauro Moraes
as I see in "path"
/classes
?– Alan PS
For example: if you are working on localhost this would be: "localhost/classes" path is the directory within your domain... at least that’s what you defined when you had the cookie saved in "/classes"
– Lauro Moraes
@Lauromoraes, it would be something like this
$_COOKIE['localhost/classes/log']
?– Alan PS
No. You set the cookie by the name you set ex: $_COOKIE['log']. Note that: when you saved the cookie you "told" it belongs to the "your.com/classes" directory if you actually have this directory in your project/site blz just go to "your.com/classes" domain and in the index of this directory check if the cookie exists. If you don’t have this directory, then save the cookie to "/"
– Lauro Moraes
You have a subdirectory called "classes" ?
– Lauro Moraes
have! saved the cookie in "/" and it worked, vlw!
– Alan PS
For further information only, read-only material: http://php.net/manual/en/features.cookies.php
– Lauro Moraes