简体   繁体   English

PHP setcookie无法正常工作

[英]PHP setcookie won't work

I am completely baffled by this problem. 我完全被这个问题困扰。 Setting a cookie should be the easiest thing in the world, but for whatever reason it's just not working. 设置cookie应该是世界上最简单的事情,但是无论出于什么原因,它都无法正常工作。 Currently I'm just trying to get a test-script to work. 目前,我只是想使测试脚本起作用。 It looks like this: 看起来像这样:

    $cookie_name = "user";
    $cookie_value = "John Doe";

    setcookie($cookie_name, $cookie_value, time() + 86400 * 30, "/");
    setcookie("act", "sj", time() + 86400 * 365);
    setcookie("bbba", "Hello", time() + 86400);

    echo $_COOKIE['act'];
    echo $_COOKIE['bbba'];
    echo $_COOKIE['user'];

None of these cookies will set. 这些cookie均不会设置。 Nothing will echo, and I can not find the cookies when using the inspector. 什么都不会回显,并且使用检查器时我找不到cookie。 I've tried the following: - Placing the echo $_COOKIE in another file in the same directory. 我尝试了以下操作:-将echo $ _COOKIE放在同一目录中的另一个文件中。 - With and without ob_start() and ob_flush() - Using "/", "/direcotry" and nothing at all as path - Moving the file to the root directory to see if it works there. -使用和不使用ob_start()和ob_flush()-使用“ /”,“ / direcotry”,而根本不使用任何路径-将文件移至根目录以查看其是否在那里工作。

Nothing seems to work, and I cannot see what could possibly be wrong. 似乎没有任何效果,我看不出有什么问题。 Other scripts using cookies are working on the same domain - which is located on a web hotel. 其他使用cookie的脚本在同一个域(网络酒店)上运行。

Can anyone see the problem here? 有人可以在这里看到问题吗?

Cookies will not become visible until the next loading of a page that the cookie should be visible for. 直到下一次加载该cookie的页面加载时,cookie才可见。 To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. 要测试cookie是否成功设置,请在cookie过期之前在下一个加载页面上检查cookie。 Expire time is set via the expire parameter. 过期时间是通过expire参数设置的。 A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);. 调试cookie存在的一种好方法是仅调用print_r($ _ COOKIE);。

It's from php manual. 它来自php手册。 You can set the value in $_COOKIE array by manual if you really want it in same page which's declared. 如果确实要在声明的同一页面中使用$ _COOKIE数组,则可以手动设置该值。

$_COOKIE['key'] = 'value';
echo $_COOKIE['key'];

PHP Manual setcookie PHP手册setcookie

该问题是由文档开头的空白引起的。

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

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