Why does this code work only after the 2nd page reload? Cooke is created after the first page load, but if the block for some reason does not see the cookie during the first page load. How can this be fixed? Use session is not possible. Ps: this is a simplified version of my code with the same prince of work.

setcookie("test", "tsst", time()+10, "/"); if (isset($_COOKIE['test'])) { echo "ok"; } 

    1 answer 1

    Cook is not set until the response is sent back to the client and is available on your PHP until the next request from the client after that.

    You can try this:

     setcookie('test', $value, time() + 10, "/"); $_COOKIE['test'] = $value; 

    upd: You can set $_COOKIE['test'] yourself or use an intermediate variable.

     if (isset($_COOKIE['test'])) { $value = $_COOKIE['test']; } else { setcookie('test', $value, time() + 10, "/"); } 
    • one
      thank you very much. I did not know about this particular cookie. - evseygames
    • Is it possible to somehow do this for those cookies that need to be removed? - evseygames