我有一个页面,test.php的,具有以下code:
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,具有以下code:
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的的按钮,我使用XMLHtt prequest打电话给我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,并且以后不能再修改,用/参数。
清除缓存和建议code工作之后,我清除我的cookies,从浏览器和使用的附加参数的设置方法,我能够从所有页面操作饼干!太谢谢你了!
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!!
如果您没有添加 $ PATH
值的setcookie()
,则默认为当前目录。这意味着,如果你设置从 /web/DEV/Classes/SetCookie.php
的饼干,饼干被设置为 / WEB /开发/班/
,和任何路径上面不会看到该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.
要解决这个问题,添加特定的$路径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, '/');