15

I have a page, test.php, with the following code:

<html>
    <body>
        <form>
            <script type="text/javascript"> 

                function SendCookies(){

                    if (window.XMLHttpRequest)/* code for IE7+, Firefox, Chrome, Opera, Safari */
                    { xmlhttp=new XMLHttpRequest(); }
                    else /* code for IE6, IE5 */
                    { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }

                    xmlhttp.onreadystatechange=function()
                    {
                        if (xmlhttp.readyState==4 && xmlhttp.status == 200)
                        {
                            alert('done');
                        }
                    }

                    xmlhttp.open("GET", "/web/DEV/Classes/SetCookie.php?time=" + new Date());
                    xmlhttp.send();

                }

            </script>

            <input type="text" id="txtInput" name="txtInput"/>
            <input type="button" id="btnSubmit" name="btnSubmit" value="Submit"  onclick="SendCookies()"/>
            <div id="divTest">
                <?php
                    if (isset($_COOKIE["TestCookie"])) {
                        echo $_COOKIE["TestCookie"];
                    } else {
                        echo "__Results__";
                    }
                ?>          
            </div>
        </form>
    </body>
</html>

I have a page, SetCookie.php, with the following code:

<?php 
    $var = "THIS IS  A  TEST";
    setcookie("TestCookie", $var, time()+60*60*24*30);
?>

When test.php's button is clicked, i use XMLHttpRequest to call my SetCookie.php page. The page executes, becuase if i add an echo to it, i get that in the xmlhttp response. However, TestCookie does not seem to be getting set.

If in text.php, i do the same command found in SetCookie.php, the cookie is then set accordingly for all browser sessions.

Even after i close / open the browser, the cookie remains unchanged from when i once set it in my test.php page manually.

----EDIT-----

I added:

if(!setcookie("TestCookie", "A", time()+60*60*24*30, "/")) {
    echo "FAIL";
}

to the very top of test.php, however when i reload the page, it never shows the updated cookie... because that cookie was already set without the ,"/" parameter, and cannot be modified later, with the ,"/" parameter.

After clearing the cache and working with the suggested code, i cleared my cookies from the browser and used the added parameter for the set method, i was able to manipulate the cookies from all pages!!! thank you so much!!

adam
  • 2,930
  • 7
  • 54
  • 89
  • Just a couple days ago I had code that did just this. The only difference i can think of, is that the SetCookies.php page existed in the same directory as the "??.php" page. maybe this has something to do with it? i will work with my code on that idea a little and post an update if i find anything out. – adam Apr 12 '11 at 14:10
  • Sorry, I was being stupid ;-) Turns out the only thing you can't so is set cookies FOR the request. – Rudi Visser Apr 12 '11 at 14:21
  • thats still good to know, i had no idea there was any restriction at all. =P – adam Apr 12 '11 at 14:50

2 Answers2

39

If you don't add a $path value to setcookie(), it defaults to "the current directory". This means that if you set the cookie from /web/DEV/Classes/SetCookie.php, the cookie gets set to /web/DEV/Classes/, and anything above that path won't see that cookie.

To fix this, add a specific $path to setcookie. If your app runs on the domain root (example.com), use '/'. If it's in a subfolder (example.com/myapp/), use '/myapp/'

setcookie("TestCookie", $var, time()+60*60*24*30, '/');
Joel L
  • 3,031
  • 1
  • 20
  • 33
  • 2
    Note, this will not change an already existing cookie. The old cookie must be cleared out first, if it did not have a path specified. – adam Aug 27 '13 at 16:54
5

I think you should look into the path parameter of the setcookie. Set it to "/" , so that it is accessible from across all directories/pages of the site.

Sabeen Malik
  • 10,816
  • 4
  • 33
  • 50