php setcookie 不适用于 ajax 调用
我有一个页面 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>
我有一个页面 SetCookie.php,包含以下代码:
<?php
$var = "THIS IS A TEST";
setcookie("TestCookie", $var, time()+60*60*24*30);
?>
单击 test.php 的按钮时,我使用 XMLHttpRequest 调用我的 SetCookie.php 页面。该页面会执行,因为如果我向其添加回显,我会在 xmlhttp 响应中得到它。然而,TestCookie 似乎没有得到设置。
如果在 text.php 中,我执行在 SetCookie.php 中找到的相同命令,则会为所有浏览器会话相应地设置 cookie。
即使我关闭/打开浏览器后,cookie 与我在 test.php 页面中手动设置时相比仍然保持不变。
----编辑-----
我添加:
if(!setcookie("TestCookie", "A", time()+60*60*24*30, "/")) {
echo "FAIL";
}
到 test.php 的最顶部,但是当我重新加载页面时,它永远不会显示更新的 cookie... 因为cookie 已经在没有 ,"/" 参数的情况下设置,并且以后无法使用 ,"/" 参数进行修改。
清除缓存并使用建议的代码后,我从浏览器中清除了 cookie 并使用为 set 方法添加的参数,我能够操作所有页面的 cookie!太感谢了!!
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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您没有向
setcookie()
添加$path
值,则默认为“当前目录”。这意味着,如果您从/web/DEV/Classes/SetCookie.php
设置 cookie,则 cookie 将被设置为/web/DEV/Classes/
以及以上任何内容该路径不会看到该 cookie。要解决此问题,请向 setcookie 添加特定的 $path。如果您的应用在域根 (example.com) 上运行,请使用
'/'
。如果它位于子文件夹 (example.com/myapp/) 中,请使用'/myapp/'
If you don't add a
$path
value tosetcookie()
, 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 的路径参数。将其设置为“/”,以便可以从站点的所有目录/页面访问它。
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.