javascript php setcookie 不适用于 ajax 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5636506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:51:40  来源:igfitidea点击:

php setcookie not working with ajax call

phpjavascriptajaxcookiesxmlhttprequest

提问by adam

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

我有一个页面 test.php,其中包含以下代码:

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

我有一个页面 SetCookie.php,其中包含以下代码:

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

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

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

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

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

即使在我关闭/打开浏览器之后,cookie 仍然保持不变,因为我曾经在我的 test.php 页面中手动设置过它。

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

到 test.php 的最顶部,但是当我重新加载页面时,它永远不会显示更新的 cookie...因为该 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!!

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

回答by Joel L

If you don't add a $pathvalue 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.

如果不向 中添加$pathsetcookie(),则默认为“当前目录”。这意味着,如果您从 设置 cookie /web/DEV/Classes/SetCookie.php,则 cookie 将设置为/web/DEV/Classes/,并且该路径之上的任何内容都不会看到该 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/'

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

setcookie("TestCookie", $var, time()+60*60*24*30, '/');

回答by Sabeen Malik

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.

我认为您应该查看 setcookie 的路径参数。将其设置为 "/" ,以便可以从站点的所有目录/页面访问它。