【发布时间】:2023-03-12 02:21:01
【问题描述】:
我有 test.php:
<html>
<?php
if (!isset($_SESSION))
{
echo 'session is not yet set';
session_start();
$_SESSION['comments'] = array();
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<input type="text" name="comment">
Click the button!
<input type="submit"/>
</form>
<?php
array_push($_SESSION['comments'], $_GET['comment']);
echo 'Current array count: ' . count($_SESSION['comments']) . '<br>';
foreach($_SESSION['comments'] as $comm)
{
echo $comm.'<br>';
}
?>
</html>
应该发生的是,在文本框中输入的任何文本都应该被推送到会话数组,但是每次我点击提交时,打印的是我刚刚输入的文本,而不是附加/存储以前的条目。我一直只得到1 作为数组计数。
每次我点击提交时,session is not yet set 总是显示在页面顶部,即使我有 if 条件。
我似乎找不到问题所在?
【问题讨论】:
-
把 session_start();在页面顶部。