Reading a cookie located in another directory


Good I have a problem with an exercise when visualizing the created cookies.

I have the following directory tree:
- / (the default directory where php hosts)
- / level
- / level / level2

So the problem is that if I create a cookie by putting it as path / level1 or / level1 / level2 I am not able to visualize these cookies, but I can visualize the ones with path "/"

The code with which I create the cookie:

if (isset($_POST['envio'])) { 
    $nombre     = sanearDato($_POST['nombre']);
    $contenido  = sanearDato($_POST['contenido']);
    $nivel      = sanearDato($_POST['nivel']);

    if (empty($nombre))     $nombre     = "NONE";
    if (empty($contenido))  $contenido  = "NONE";

    switch($nivel) {
        case "0": $nivel = "/"; break;
        case "1": $nivel = "/nivel1"; break;
        case "2": $nivel = "/nivel1/nivel2"; break;

    setcookie($nombre, $contenido."_".$nivel, time()+60*60, $nivel);

When I visualize the $ _COOKIE with var_dump it does not show me any cookies to which I have given a different path to "/", but those that have it.

Is there any way to be able to visualize those cookies that have path / level1 or / level1 / level2?


asked by Vasyl Havrylyuk 20.11.2018 в 13:39

1 answer


The problem you are having is the path that you add. When you create a cookie with a path , it will be visible when you are on that route or on a route less than this one.

That is, if you create a cookie with path "/" , that path and those below can see the cookie. If you create it in "/nivel1" , the cookie can be seen in "/nivel1" and "/nivel1/nivel2" but not in the root. Finally, if you create it in "/nivel1/nivel2" , it will only be seen in that route.

To be able to create a table with all the cookies you should do it from the element farthest from the root, since from here all cookies are accessible.

answered by 20.11.2018 в 14:04