<?php
session_start();
$_SESSION['page'] = "test.php";
echo "Page to load in iframe = ". $_SESSION['page'];
setcookie('PagetoShow', $_SESSION);
if(isset($_COOKIE['PagetoShow']))
$Page = $_COOKIE['PagetoShow'];
echo "Page to load in iframe = ". $Page;
else
$Page="welcome.php"
?>
我想这是一个语法错误,但我知道太少或没有PHP找到它。有什么问题?
答案 0 :(得分:2)
<?php
session_start();
$_SESSION['page'] = "test.php";
很好,至少你不要忘记在设置会话密钥之前运行session_start()
。
echo "Page to load in iframe = ". $_SESSION['page'];
setcookie('PagetoShow', $_SESSION);
这是有问题的。 Cookie是在标题字段中发送的,但您已通过回显某些内容来刷新标题。
if(isset($_COOKIE['PagetoShow']))
$Page = $_COOKIE['PagetoShow'];
echo "Page to load in iframe = ". $Page;
else
$Page="welcome.php"
此处存在语法错误,您需要为if-body添加括号:
if(isset($_COOKIE['PagetoShow'])) {
$Page = $_COOKIE['PagetoShow'];
echo "Page to load in iframe = ". $Page;
}
一些文档:
答案 1 :(得分:0)
setcookie
的第二个参数需要是一个字符串 - 你传递一个数组($ _SESSION)。你可以这样做:
$_SESSION['page'] = "test.php";
setcookie('PagetoShow', $_SESSION['page']); // <-- this is the only change needed
if(isset($_COOKIE['PagetoShow']))
$Page = $_COOKIE['PagetoShow'];
echo "Page to load in iframe = ". $Page;
else
$Page="welcome.php"