php setcookie 在 php5 下失败

发布于 2024-08-20 15:46:18 字数 993 浏览 6 评论 0原文

我创建了这个简单的脚本,它将设置一个具有三个值的 cookie,或者检索 cookie 值(如果已设置)。在我的运行 PHP4 的服务器上,一切正常。在我的 PHP 5 (5.2.11) 服务器上,脚本无法在浏览器中设置 cookie。我已经检查过我的 php.ini 中是否启用了输出缓冲,并且确实如此。有谁知道为什么这不起作用?

<?php 
echo "<!DOCTYPE html>";
echo "<body>";
if (!isset($_COOKIE['taeinv'])) {
    echo "No cookie set...   Attempting to set a new cookie.";
    $user = "testuser";
    $role = "admin";
    $expire = "true";
    $halfHour = 1800;
    setcookie("websitename[Expire]", $expire, time()+$halfHour);
    setcookie("websitename[User]", $user, time()+$halfHour);
    setcookie("websitename[Role]", $role, time()+$halfHour);
}
if (isset($_COOKIE['websitename'])) {
    echo "Cookie Values:";
    echo "<br />";
        foreach ($_COOKIE['websitename'] as $name => $value) {
            echo "<b>$name</b> : $value <br />\n";
        }
}
echo "<br />";
echo "<a href=logout.php>Logout</a>";
echo "</body>";
echo "</html>";
?>

I created this simple script which will either set a cookie with three values or retrieve the cookies values if they are already set. On my server running PHP4, everything works. On my server with PHP 5 (5.2.11), the script fails to set the cookie in the browser. I already checked if output buffering is enabled in my php.ini and it is. Does anyone have any ideas as to why this fails to work?

<?php 
echo "<!DOCTYPE html>";
echo "<body>";
if (!isset($_COOKIE['taeinv'])) {
    echo "No cookie set...   Attempting to set a new cookie.";
    $user = "testuser";
    $role = "admin";
    $expire = "true";
    $halfHour = 1800;
    setcookie("websitename[Expire]", $expire, time()+$halfHour);
    setcookie("websitename[User]", $user, time()+$halfHour);
    setcookie("websitename[Role]", $role, time()+$halfHour);
}
if (isset($_COOKIE['websitename'])) {
    echo "Cookie Values:";
    echo "<br />";
        foreach ($_COOKIE['websitename'] as $name => $value) {
            echo "<b>$name</b> : $value <br />\n";
        }
}
echo "<br />";
echo "<a href=logout.php>Logout</a>";
echo "</body>";
echo "</html>";
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

两相知 2024-08-27 15:46:18

您必须在向浏览器输出任何内容之前设置 cookie。尝试将所有 echo 行移至 setcookie 调用下方的某个位置。你可以这样做:

<?php
$set = false;
if (!isset($_COOKIE['taeinv'])) {
    $set = true;
    $user = "testuser";
    $role = "admin";
    $expire = "true";
    $halfHour = 1800;
    setcookie("websitename[Expire]", $expire, time()+$halfHour);
    setcookie("websitename[User]", $user, time()+$halfHour);
    setcookie("websitename[Role]", $role, time()+$halfHour);

}
echo "<!DOCTYPE html>";
echo "<body>";
if ($set) {
    echo "No cookie set...   Attempted to set a new cookie.";
}
if (isset($_COOKIE['websitename'])) {
    echo "Cookie Values:";
    echo "<br />";
        foreach ($_COOKIE['websitename'] as $name => $value) {
            echo "<b>$name</b> : $value <br />\n";
        }
}
echo "<br />";
echo "<a href=logout.php>Logout</a>";
echo "</body>";
echo "</html>";
?>

You have to set the cookie before any output to the browser. Try moving all echo lines somewhere below the setcookie call. You could do something like this:

<?php
$set = false;
if (!isset($_COOKIE['taeinv'])) {
    $set = true;
    $user = "testuser";
    $role = "admin";
    $expire = "true";
    $halfHour = 1800;
    setcookie("websitename[Expire]", $expire, time()+$halfHour);
    setcookie("websitename[User]", $user, time()+$halfHour);
    setcookie("websitename[Role]", $role, time()+$halfHour);

}
echo "<!DOCTYPE html>";
echo "<body>";
if ($set) {
    echo "No cookie set...   Attempted to set a new cookie.";
}
if (isset($_COOKIE['websitename'])) {
    echo "Cookie Values:";
    echo "<br />";
        foreach ($_COOKIE['websitename'] as $name => $value) {
            echo "<b>$name</b> : $value <br />\n";
        }
}
echo "<br />";
echo "<a href=logout.php>Logout</a>";
echo "</body>";
echo "</html>";
?>
旧街凉风 2024-08-27 15:46:18

这在我的旧 PHP4 服务器上有效,但在 PHP5 上无效。

That worked on my old PHP4 server, but not on PHP5.

站稳脚跟 2024-08-27 15:46:18

使用输出缓冲ob_start()ob_end_flush()

示例:

<?php
    ob_start();
    echo '<p>Initializing…</p>';
    setcookie('myLanguage', 'PHP');
    ob_end_flush();
    // you can continue your PHP code here…
?>

Use output bufferingob_start() and ob_end_flush().

Example:

<?php
ob_start();
echo '<p>Initializing…</p>';
setcookie('myLanguage', 'PHP');
ob_end_flush();
// you can continue your PHP code here…
?>

洋洋洒洒 2024-08-27 15:46:18

我也遇到了类似的问题,但只有在 Chrome 中,cookies 才消失。火狐浏览器很好。

设置 setcookie 函数中的所有参数修复了该问题。

这会设置 cookie,但 Chrome 会在一次点击中删除 cookie:

setcookie('uname', 'Joe', time()+3600*24);

这会设置 cookie 并且浏览器会保留它:

setcookie('uname', 'Joe', time()+3600*24, '/', 'www.domain.com', false, false);

I had a similar problem but it was only in Chrome that the cookies disappeared. Firefox was fine.

Setting all the parameters in the setcookie function fixed it.

This sets the cookie but Chrome drops the cookie within a click:

setcookie('uname', 'Joe', time()+3600*24);

This sets the cookie and the browser retains it:

setcookie('uname', 'Joe', time()+3600*24, '/', 'www.domain.com', false, false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文