-3

可能重复:
PHP 中的“警告:标头已发送”

<?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 知之甚少或一无所知。怎么了?

4

2 回答 2

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;
}

一些文档:

于 2012-11-13T22:05:12.740 回答
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"
于 2012-11-13T21:52:26.853 回答