It is not clear where you are invoking isset()
. So the answer assumes you are on the same page where you invoked setcookie()
.
In this case it will not be accessible unless you explicitly declare 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 happens because the global variable $_COOKIE
with the index "log" will only be available on the next request.
To make it available on the same page where it is created, it should be explicitly stated:
setcookie('log', true, time()+60, '/classes');
$_COOKIE['log'] = true;
Note: This behavior varies depending on the browser and its version. Of course, always seven explicitly the global variable.
To make it more practical and reusable, create a routine:
function CookieSet($k, $v) {
setcookie($k, $v, time()+60, '/classes');
$_COOKIE[$k] = $v;
}
CookieSet('log', true);
Obviously check that the fourth parameter of setcookie()
is correct
You are setting '/classes'
. You may want to use '/'
.
Setting to '/classes'
, you are limiting cookies to the '/classes'
directory. ex: http://localhost/classes
. If you access http://localhost/
, they will not be available.