PHP - Cookies, how to detect if you are created

Asked

Viewed 1,851 times

0

How do I detect if a cookie exists ?

My cookie is created like this:

setcookie("log", "true", time()+60, "/classes");

And to detect I’m trying it:

if (!isset($_COOKIE['log'])) {
   //função
}

But it does not detect the cookie, it runs the function within if even if the cookie exists..

Follow the cookie data in Chrome:

Nome:   log
Conteúdo:   true
Caminho:    /classes
Enviar para:    Qualquer tipo de conexão
Acessível ao script:    Sim
Criado em:  segunda-feira, 12 de setembro de 2016 14:05:10
  • 1

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

  • as I see in "path" /classes ?

  • 1

    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"

  • @Lauromoraes, it would be something like this $_COOKIE['localhost/classes/log'] ?

  • 1

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

  • 1

    You have a subdirectory called "classes" ?

  • have! saved the cookie in "/" and it worked, vlw!

  • 1

    For further information only, read-only material: http://php.net/manual/en/features.cookies.php

Show 3 more comments

2 answers

1

Well you’re doing a validation if THERE IS NO your Cookie, therefore, your function will be executed only if the cookie is not created, remembering that "!" in the validation makes the inverse of the cogido "isset". And see if really exite the directory /classes in your project where you saved this cookie. I hope I helped and good luck!

-2

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.