简体   繁体   中英

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. 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. Nothing will echo, and I can not find the cookies when using the inspector. I've tried the following: - Placing the echo $_COOKIE in another file in the same directory. - 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.

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.

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. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.

It's from php manual. You can set the value in $_COOKIE array by manual if you really want it in same page which's declared.

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

PHP Manual setcookie

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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