session_start()在保存变量时抛出错误

时间:2013-06-13 20:20:51

标签: php html

我有以下表格:

<html>
<head>
</head>

<body>
<form method=post action="submit.php">
<!-- 


If the user goes to a different page or refreshes this page it and returns to this page within 2 hours the value that
was originally entered should populate in the respective textbox,radio groups which is not happening...

 -->
    <input type=text value="<?php echo $_SESSION['saveit'][0]; ?>" name="first" />

    <input type=radio value="A" name="acct" <?php echo ($_SESSION['saveit'][1] == A) ? "checked" : "" ?> /> A
    <input type=radio value="B" name="acct" <?php echo ($_SESSION['saveit'][1] == B) ? "checked" : "" ?> /> B

    <input type=radio value="A" name="birt" <?php echo ($_SESSION['saveit'][2] == A) ? "checked" : "" ?> /> A
    <input type=radio value="B" name="birt" <?php echo ($_SESSION['saveit'][2] == B) ? "checked" : "" ?> /> B

    <input type=submit value="submit" />
</form>
<?php
//echo to see what those values are
echo $_SESSION['saveit'][0];
echo "<BR>";
echo $_SESSION['saveit'][1];
echo "<BR>";
echo $_SESSION['saveit'][2];
echo "<BR>"
?>
</body>
</html>

以下PHP页面:

<?php
session_start();

//Get the value from form
$first = trim(strip_tags(stripslashes($_POST['first'])));
$acct = trim(strip_tags(stripslashes($_POST['acct'])));
$birt = trim(strip_tags(stripslashes($_POST['birt'])));

$f = isset($first) ? $first : null;
$a = isset($acct) ? $acct : null;
$b = isset($birt) ? $birt : null;

$savearray = array($f,$a,$b);

$_SESSION['saveit'] = $savearray;

//save the value in each individual session (Not using it as I need to save multiple values in ONE session)
//$_SESSION['textbox1'] = isset($first) ? $first : null;
//$_SESSION['radiogroup1'] = isset($acct) ? $acct : null;
//$_SESSION['radiogroup2'] = isset($birt) ? $birt : null;

//set the cookie for each session with the value for 2 hours TTL
//CORRECT syntax to save the ONE session with multiple variable into cookie to be used for upto 2 hours MAX.
//setcookie("textbox1", $first, time()+(3600*2));

//echo test to see what the values are
echo $_SESSION['saveit'][0];
echo "<BR>";
echo $_SESSION['saveit'][1];
echo "<BR>";
echo $_SESSION['saveit'][2];
echo "<BR>";

echo "<a href='http://www.interfaithmedical.com/checksite/testFolder/form.php'>GO TO THE FORM PAGE</a>";
?>
  • 尝试使用setcookie()将会话中的每个变量存储为仅仅2小时,我该如何实现?
  • 当我重新加载FORM页面时,不会使用会话自动输入值。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您无法在第一页上调用session_start()。必须在使用会话变量的所有页面上调用它,并且应该先调用它。

实际上,您从未在包含表单的页面上启动会话,因此您设置的任何值实际上都没有设置。