【发布时间】: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/… -
非常感谢迈克尔!我理解我的错误。