header无法传递和获取参数

时间:2013-07-19 18:11:57

标签: php parameters header

我使用header传递参数,但没有工作

这是第1页:     

session_start();

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();
$problem  = "correct";

header('Location:trynew2.php?problem=$problem');

?>

这是我的第二页:

<?php
session_start();

echo "welcome" ;
$problem = $_GET['problem'];
echo $problem;

$test = $_SESSION['favcolor'];
echo $test;
?>

结果是欢迎$ problemgreen

2 个答案:

答案 0 :(得分:2)

使用单引号只是回显你放在那里的内容,并且不处理变量。您需要在标题行上使用双引号

header("Location:trynew2.php?problem=$problem");

答案 1 :(得分:0)

header('Location:trynew2.php?problem=$problem');

这会将您转到下一页,网址为:

trynew2.php?problem=$problem

单引号不插入变量;如果您希望此操作符合预期,则需要使用双引号:

header("Location:trynew2.php?problem=$problem");
相关问题