会话变量似乎没有被保存 [英] Session variables seem not to be saved
问题描述
相当简单的代码:
<?
session_start();
$_SESSION['t'.time()] = "ok";
echo "<pre>".print_r($_SESSION, 1)."</pre>";
?>
正如预期的那样显示
Array
(
[t1330966834] => ok
[t1330966835] => ok
[t1330966836] => ok
)
重新加载 3 页后.
让我们改变一些符号:
$_SESSION[time()] = "ok";
(现在没有 't'),我希望在几次重新加载后像
(now without 't') and I expect after few reloads something like
Array
(
[t1330966834] => ok
[t1330966835] => ok
[t1330966836] => ok
[1330967020] => ok
[1330967021] => ok
[1330967022] => ok
[1330967023] => ok
)
但实际上结果完全不同:
But actually the result is absolutely different:
Array
(
[t1330966834] => ok
[t1330966835] => ok
[t1330966836] => ok
[1330967020] => ok
)
我们有 3 个先前的数组单元格和一个且只有一个时间"单元格 - 无论您重新加载页面多少次.时间是正确的,它每秒都不同,但只有一个没有t"的单元格!我也试过
We have 3 previous array cells ad one and only one 'time' cell - no matter how many times you reload the page. The time is correct, it different each second but only one cell without 't'! Also I tried
$t =time();
$_SESSION[$t] = "ok";
甚至
$t =intval(time());
$_SESSION[$t] = "ok";
但随着时间的推移,它只剩下一个细胞.
But it's remains only one cell with time.
在 php 5.2.13 和 5.3.10 上在 2 个不同的服务器上进行了测试.我做错了什么?
Tested at php 5.2.13 and 5.3.10 at 2 different servers. What am I doing wrong?
推荐答案
$_SESSION
关联数组中的键与 PHP 中的常规变量名称具有相同的限制,即它们不能以数字开头,必须以字母或下划线开头.有关更多详细信息,请参阅本手册中有关变量的部分.
The keys in the
$_SESSION
associative array are subject to the same limitations as regular variable names in PHP, i.e. they cannot start with a number and must start with a letter or underscore. For more details see the section on variables in this manual.
http://php.net/manual/en/session.examples.basic.php一个>
这篇关于会话变量似乎没有被保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!