简体   繁体   English

setcookie()无法正常工作?

[英]setcookie() doesn't work properly?

Hi I'm trying to use cookies on my site. 嗨,我正在尝试在我的网站上使用Cookie。 I created two test pages to try them out before I implement them in my site, but it doesn't seem to work well. 我创建了两个测试页来对其进行尝试,然后再在网站上实施它们,但这似乎效果不佳。

page one has: 第一页有:

<?php
setcookie("apple", "abc", time()+1500, "/", "b****a.org");
?>

page two has: 第二页有:

<?php
echo $_COOKIE["apple"];
?>

So to test it, I go to page one first, then I go to page two. 因此,要进行测试,我先转到第一页,然后再转到第二页。 For some reason, page two just outputs the letter "b" on the screen. 由于某种原因,第二页只是在屏幕上输出字母“ b”。 That's it. 而已。 I tried it on another computer, and it outputs "abcb" what's this b for? 我在另一台计算机上尝试过,它输出“ abcb”,这b代表什么? I'm guessing the cookie isn't getting stored on my computer, but it does work on two other computers so I guess it's just my problem. 我猜测cookie不会存储在我的计算机上,但它确实可以在另外两台计算机上工作,所以我想这只是我的问题。 Anyways, why does it output an extra "b" at the end? 无论如何,为什么它最后会输出一个额外的“ b”?

Thanks. 谢谢。

像这样设置cookie对我来说很好。

setcookie("apple", "abc", time()+1500);

There's some problem with your path or domain name, I used this on my local and it works perfect.. 您的路径或域名有问题,我在本地使用了它,效果很好。

<?php
setcookie("mycookie", "This cookie tastes good", time()+3600, "", "localhost");

echo $_COOKIE['mycookie'];   //output : This cookie tastes good

//refresh twice if you are setting a cookie and using echo on a same page
?>

For more info on cookie : PHP Manual 有关cookie的更多信息: PHP手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM