duandie0884 2011-09-09 21:47
浏览 20
已采纳

PHP会话看起来很糟糕

This is used to get a list of previously served markers from session:

session_start();
if(!isset($_SESSION['markers'])) {
    $_SESSION['markers'] = array(0);
    $list = '0';
} else {
    $list = implode(', ', $_SESSION['markers']);
}

I use $list to get markers not already served:

SELECT * FROM `markers` WHERE {other conditions} AND `marker_id` NOT IN($list) 

Then for each row returned I add the marker id to session:

while ($row = mysqli_fetch_assoc($result)){
  $_SESSION['markers'][] = (int) $row['marker_id'];
  ...
}

However, after serving some markers (say 8, 36) the session looks corrupt. var_dump($_SESSION); outputs:

array(1) {
  ["markers"]=>
  &array(3) {
    [0]=>
    int(0)
    [1]=>
    int(8)
    [2]=>
    int(36)
  }
}

I say it is corrupt because when I manually make a similar array (codepad here) the output does not show a & in the third line - it is array(3), not &array(3). What is causing this?

Update
@Marc B is hinting the session is not corrupt. His take is that we just have a variable ($_SESSION), containing a reference to an array ($markers), instead of the array itself; and that is not a corrupt variable. Well, the session is corrupt for session usage purposes. Two signs:

  1. A second call to the same script (in which I guess the else route is taken) results in a fatal error: Fatal error: Cannot use object of type DOMElement as array in ... on line 47. Line 47 is where we attempt to add a new marker id to the array: $_SESSION['markers'][] = (int) $row['marker_id'];
  2. When I try to unserialize the actual contents of session file it fails (codepad here).
  • 写回答

2条回答 默认 最新

  • doushou6480 2011-09-09 23:31
    关注

    If you have register_globals to On, global variables associated to $_SESSION variables are references. Source: http://php.net/manual/en/reserved.variables.session.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比