在构造函数和析构函数中读取会话变量的 I/O

发布于 2024-12-05 21:17:06 字数 560 浏览 1 评论 0原文

我有一个页面在循环中被多次调用。我有一个版本,它在页面加载时从 MySQL 中提取数据,在页面再次重复该过程之前将修改后的数据推送回每次提交。其中一些数据仅对会话的运行很重要,在会话结束时转储

我正在考虑一个类的想法,该类从构造函数中的会话变量加载其变量,然后将最终值推回到析构函数中的相同会话变量。我有一些类似于以下内容的内容。变量在第一次调用构造函数时成功初始化,并由析构函数的第一个实例传递出去。但是,它们在第二次调用构造函数时无法加载。我错过了什么吗?

提前致谢

class counters
{
    protected $qCounters;

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

    // process happen here to alter values

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

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 running 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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2024-12-12 21:17:06

这是我的[非常简单]会话处理程序。注意 db_query() 是 mysqli_query() 的简单包装器,并在内部处理所有连接详细信息。

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;
}

参考资料

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文