开发者

reading I/O of session variables in constructor and destructor

I have a page that gets called many times in a loop. I have a version which is pulling data from MySQL on page load, pushing modified data back with each submission before the page repeats the process again. Some of this data is important only to the ru开发者_开发百科nning of the session, being dumped at session end

I'm playing with the idea of a class which loads its variables, from session variables in the constructor, then pushes the final values back out to the same session variables in the destructor. I have something along the lines of the following. The variables are successfully initialised on first call to constructor, and passed out by first instance of destructor. However, they fail to be loaded on second call to constructor. Am I missing something?

thanks in advance

class counters
{
    protected $qCounters;

    function __construct()
    {
    $this->qCounters = $_SESSION['q']['counters'];
    }

    // process happen here to alter values

    function __destruct()
    {
    $_SESSION['q']['counters'] = $this->qCounters;
    }
}


Here's my [very simple] session handler. Note db_query() is a simple wrapper for mysqli_query() and handles all the connection details internally.

function session_close() {
  return true;
}

function session_die($id) {
  db_query("DELETE FROM session WHERE ID='$id'");
  return true;
}

function session_gc($maxlifetime) {
  return true;
}

function session_open($path,$name) {
  return true;
}

function session_read($id) {
  $dchk = db_query("SELECT data FROM session WHERE ID='$id'");
  if(db_numrows($dchk) == 1) {
    if(!isset($_SESSION['row'])) { $_SESSION['row'] = 1; }
    list($data) = db_rows($dchk);
    return base64_decode($data);
  } else {
    return "";
  }
  db_free($dchk);
  return true;
}

function session_write($id,$data) {
  global $visitor;
  $data = base64_encode($data);
  if(!isset($_SESSION['row'])) {
    db_query("INSERT IGNORE INTO session (ID,data,accessed) VALUES('$id','$data',UNIX_TIMESTAMP(NOW()))");
  } else {
   db_query("UPDATE session SET data='$data',accessed=UNIX_TIMESTAMP(NOW()) WHERE ID='$id'");
  }
  return true;
}

References

  • Doc Bug #27555: Unable to modify $_SESSION from __destruct()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜