0

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

第 1 页代码:

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

第2页代码:

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

1 回答 1

0

手册

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>';
?>
于 2013-07-18T17:39:51.417 回答