易语言mysql偶尔写入失败_php session偶尔写入失败的原因

session_start();var_dump($_SESSION);$key = sprintf('%05d', mt_rand(0, 99999));$key = strval($key);$val = time();$_SESSION[$key] = $val;

这是我的代码,经测试后大部分情况下无法输出刚才设置的session,那就是写入失败了。

我百思不得其解,如果说失败的话都要失败,为何偶尔还有成功的呢?

后来单独测试的时候把error_reporting 全部打开了,发现提示这样的错误

Notice: Unknown: Skipping numeric key 454352 in Unknown on line 0

经查证,为如下原因

The PHP session storage mechanism was originally built around "registering" variables, so the keys in $_SESSION must be names that could be treated as variables in their own right.

This means that $_SESSION[42] is invalid, because $42 wouldn't be a valid variable name, and since $foo[42] and $foo['42'] refer to the same thing, $_SESSION['42'] is invalid as well.

The solution is either to use a prefix on your session variables (e.g. $_SESSION['row_count_' . $x] = $row['Count'];) or make them into an array, which you can then loop over etc later (e.g. $_SESSION['row_counts'] = array(); ... $_SESSION['row_counts'][$x] = $row['Count'];)

Note: this limitation is actually part of the "serialization handler" used when writing the session to disk, which is why the errors have no context (they're fired while PHP is shutting down). In very recent versions of PHP there is a setting of session.serialize_handler which doesn't have this limitation

309007 6126gvsg 18616126356 1422322650 v5pdyh9S7eyD

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值