php setcookie不能使用ajax调用

时间:2021-02-28 01:25:55

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

我有一个页面,测试。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.

当测试。单击php的按钮,我使用XMLHttpRequest调用我的SetCookie。php页面。这个页面执行,因为如果我向它添加一个echo,就会在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.

如果在文本。php,我执行SetCookie中的相同命令。然后,针对所有浏览器会话相应地设置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仍然与我在测试中设置它时保持不变。手动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.

到测试的最顶端。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 !!非常感谢! !

2 个解决方案

#1


31  

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.

如果不向setcookie()添加$path值,则默认为“当前目录”。这意味着如果您从/web/DEV/Classes/SetCookie设置cookie。php, cookie被设置为/web/DEV/类/,路径上的任何东西都看不到那个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添加一个特定的$路径。如果你的应用程序在域根(example.com)上运行,使用'/'。如果它在子文件夹(example.com/myapp/)中,使用'/myapp/'

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

#2


3  

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的路径参数。将它设置为“/”,以便从站点的所有目录/页面访问它。

#1


31  

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.

如果不向setcookie()添加$path值,则默认为“当前目录”。这意味着如果您从/web/DEV/Classes/SetCookie设置cookie。php, cookie被设置为/web/DEV/类/,路径上的任何东西都看不到那个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添加一个特定的$路径。如果你的应用程序在域根(example.com)上运行,使用'/'。如果它在子文件夹(example.com/myapp/)中,使用'/myapp/'

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

#2


3  

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的路径参数。将它设置为“/”,以便从站点的所有目录/页面访问它。