php setcookie 不适用于 ajax 调用

2022-01-15 00:00:00 cookies xmlhttprequest php javascript ajax

我有一个页面 test.php,代码如下:

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>

我有一个页面,SetCookie.php,代码如下:

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

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

当点击 test.php 的按钮时,我使用 XMLHttpRequest 来调用我的 SetCookie.php 页面.页面执行,因为如果我向它添加回声,我会在 xmlhttp 响应中得到它.但是,似乎没有设置 TestCookie.

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.

如果在 text.php 中,我执行在 SetCookie.php 中找到的相同命令,然后为所有浏览器会话设置相应的 cookie.

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

即使在我关闭/打开浏览器之后,cookie 与我曾经在我的 test.php 页面中手动设置时一样保持不变.

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

----编辑-----

我补充说:

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

到 test.php 的最顶部,但是当我重新加载页面时,它永远不会显示更新的 cookie... 因为该 cookie 已经在没有/"参数的情况下设置,并且以后无法修改, 带有 ,"/" 参数.

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.

在清除缓存并使用建议的代码后,我从浏览器中清除了我的 cookie,并为 set 方法使用了添加的参数,我能够从所有页面操作 cookie!非常感谢!!

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!!

推荐答案

如果不给 setcookie() 添加 $path 值,则默认为 "当前目录".这意味着如果您从 /web/DEV/Classes/SetCookie.php 设置 cookie,则 cookie 将设置为 /web/DEV/Classes/ 以及以上任何内容该路径不会看到该 cookie.

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.

要解决此问题,请将特定的 $path 添加到 setcookie.如果您的应用在域根目录 (example.com) 上运行,请使用 '/'.如果它位于子文件夹 (example.com/myapp/) 中,请使用 '/myapp/'

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, '/');

相关文章