【问题标题】:Cookie does not work on server whereas are OK on localhostCookie 在服务器上不起作用,而在 localhost 上正常
【发布时间】:2021-06-03 10:53:30
【问题描述】:

首先:

  • 我用 HTML 和 PHP 开发了一个网站。
  • 我在 Chrome 下使用 Laragon 4.0.16(Apache 服务器 httpd-2.4.36-win64-VC15 和 PHP 7.2.19)进行本地测试。
  • 该文件是在 Notepad++ 下编写的,为 UTF-8

我的目标是保存一个在网站不同页面之间共享的变量。为此,我使用setcookie。在本地主机上可以,但在主机(ionos.fr)上不行。

我做了一个简单的测试来检查错误。

在本地主机下

1/ 在 index.php 上,我显示 $_COOKIE(第一次只有 PHPSESSID)。

2/ 当点击按钮“SetChapter”时,我调用一个 php (call_cookie_xx.php) 将 cookie 设置为值 16 或 33(取决于按下的按钮)。这个php只做setcookie然后打开一个子页面sub_index.php

3/ 在 index_sub.php 上,我通过调用echo <script>XXX</script> 读取了$_COOKIE(由call_cookie_xx 设置)。刷新 cookie(flush_cookie.php)或返回主页。

4/ 我在从 sub_index.php 回到 index.php 时定义了 $_COOKIE[Chapter] => 没关系 - 例如:

Array ( [PHPSESSID] => pcv4f5d2fvm58ruvl9ddn59l61 [chapter] => 33 )

远程主机下

setcookie 和页面切换(使用header...)都不执行任何操作。甚至没有PHPSESSID

在网上查了很多资料,没找到原因。

有人有什么想法吗?

  • 我检查了文件编码
  • setcookie 是文件call_cookie_xx 的第一条指令

这是我的示例代码

index.php:

<?php 
print_r($_COOKIE);
?>

<!DOCTYPE html>
<html lang="">

<head>
  <title>test</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>

<?php
function popup_echo($text)
{
    $msg = "\"" . $text . "\"";
    $echo_cmd = '<script type="text/javascript">alert(' .$msg . ');';
    $echo_cmd .= '</script>';
    echo $echo_cmd;
}
function get_chapter()
{
    if(!isset($_COOKIE["chapter"])) 
    {
        popup_echo("_COOKIE[chapter] no set");
        return 0;
    } 
    else 
    {   
        popup_echo("_COOKIE[chapter]=".strval($_COOKIE["chapter"]));
        return intval($_COOKIE["chapter"]);
    }
}


if (isset($_POST['GetCookie']))
{
    get_chapter();
}
elseif (isset($_POST['go_index1']))
{
    popup_echo("_POST[go_index1]");
    header("Location: ./call_cookie_16.php");   //will set $_COOKIE["chapter"]=16 && call sub_index.php
}
elseif (isset($_POST['go_index2']))
{
    popup_echo("_POST[go_index2]");
    header("Location: ./call_cookie_33.php");   //will set $_COOKIE["chapter"]=33 && call sub_index.php
}
?>
<body>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
        <div class="form-group">
            <input type="submit" class="btn btn-primary" name="GetCookie" value="GetCookie[Chapter]"> <br>
            <input type="submit" class="btn btn-primary" name="go_index1" value="Set Chapter to 16 and go to subpage">
            <input type="submit" class="btn btn-primary" name="go_index2" value="Set Chapter to 33 and go to subpage">
        </div>
    </form>
</body>

</html>

sub_index.php

<?php 
print_r($_COOKIE);
?>

<!DOCTYPE html>
<html lang="">

<head>
  <title>test</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>

<?php
function popup_echo($text)
{
    $msg = "\"" . $text . "\"";
    $echo_cmd = '<script type="text/javascript">alert(' .$msg . ');';
    $echo_cmd .= '</script>';
    echo $echo_cmd;
}
function get_chapter()
{
    if(!isset($_COOKIE["chapter"])) 
    {
        popup_echo("_COOKIE[chapter] no set");
        return 0;
    } 
    else 
    {   
        popup_echo("_COOKIE[chapter]=".strval($_COOKIE["chapter"]));
        return intval($_COOKIE["chapter"]);
    }
}


if (isset($_POST['GetCookie']))
{
    get_chapter();
}
elseif (isset($_POST['FlushCookie']))
{
    header("Location: ./flush_cookie.php");
}
elseif (isset($_POST['go_index']))
{
    popup_echo("go to index");
    header("Location: ./index.php");
}
?>
<body>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
        <div class="form-group">
            <input type="submit" class="btn btn-primary" name="GetCookie" value="GetCookie[Chapter]"> <br>
            <input type="submit" class="btn btn-primary" name="FlushCookie" value="FlushCookie[Chapter]"> <br>
            <input type="submit" class="btn btn-primary" name="go_index" value="Go back to index.php">
        </div>
    </form>
</body>

</html>

在 local_host 上,我使用 null 而不是“cma-accordions.com”

call_cookie_16.php

<?php
$ret = setcookie('chapter', "16", time() + 365*24*3600, '/', "cma-accordions.com", false, true);
header("Location: ./sub_index.php");
?>

call_cookie_33.php

<?php
$ret = setcookie('chapter', "33", time() + 365*24*3600, '/', "cma-accordions.com", false, true);
header("Location: ./sub_index.php");
?>

flush_cookie.php

<?php
$ret = setcookie('chapter', "0", time() - 360, '/', "cma-accordions.com", false, true);
header("Location: ./sub_index.php");
?>

【问题讨论】:

  • 您在 html 标记中间调用 header() 肯定会导致 Headers already sent... 错误,cookie 也会受到这些错误的影响。在测试和调试error_reporting(E_ALL); ini_set('display_errors', 1); 时始终位于脚本的顶部,您需要重新构建代码,以便在调用header() 之前不发送任何类型的输出。 stackoverflow.com/questions/8028957/…
  • 非常感谢迈克尔!我理解我的错误。

标签: php cookies


【解决方案1】:

已解决! 非常感谢。它解决了 20 小时的尝试和互联网搜索!

根据您的建议,我将代码修改为:

  • 第一个 HTML 标记之前的所有 php 代码
  • 在更改页面的分支中删除调用 header 之前的所有回显和弹出窗口!

index.php(与 sub_index.php 相同)

<?php 
error_reporting(E_ALL); ini_set('display_errors', 1);

if (isset($_POST['GetCookie']))
{
    get_chapter();
}
elseif (isset($_POST['go_index1']))
{
    header("Location: ./call_cookie_16.php");   //will set $_COOKIE["chapter"]=16 && call sub_index.php
}
elseif (isset($_POST['go_index2']))
{
    header("Location: ./call_cookie_33.php");   //will set $_COOKIE["chapter"]=33 && call sub_index.php
}

function popup_echo($text)
{
    $msg = "\"" . $text . "\"";
    $echo_cmd = '<script type="text/javascript">alert(' .$msg . ');';
    $echo_cmd .= '</script>';
    echo $echo_cmd;
}
function get_chapter()
{
    if(!isset($_COOKIE["chapter"])) 
    {
    popup_echo("_COOKIE[chapter] no set");
        return 0;
    } 
    else 
    {   
    popup_echo("_COOKIE[chapter]=".strval($_COOKIE["chapter"]));
        return intval($_COOKIE["chapter"]);
    }
}
?>

<!DOCTYPE html>
...

【讨论】:

    【解决方案2】:

    另外,

    • 注意&lt;?php 之前和?&gt; 之后的多余文本或空格
    • 删除尾随空格
    • 测试期间在浏览器中手动清除 cookie,并尝试通过嵌入式检查(如 Firefox/右键单击/检查元素/存储/Cookies)查看它们

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 2015-07-04
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多