php session无法将变量传递给其他页面

时间:2013-07-18 17:35:49

标签: php session variables

我有一个正式的代码,但会话变量无法传递给另一个页面。 所以这是我的示例代码。但是,此代码的结果为0。:

第1页代码:

<?php
session_start();
$_SESSION['try'] = 5;
header('Location: page2.php');
?>

第2页代码:

<?php
session_start();
$test = $_SESSION['try'];
echo $test;
?>

1 个答案:

答案 0 :(得分:0)

来自manual

session_start() creates a session or resumes the current 
one based on a session identifier passed via a GET or POST request, 
or passed via a cookie.

注意最后两行代码:

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

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

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>