Zend_Session:确定Session是最初启动还是刚刚更新

发布于 2024-11-06 18:46:26 字数 1058 浏览 2 评论 0原文

我的 Zend_Session 有问题。我需要知道该用户的会话是第一次启动还是刚刚在当前请求中更新。

我需要知道这一点以进行统计。如果会话已初始化(意味着用户第一次访问我的应用程序),我想将请求的引用存储在某个数据库表中。当然,我只想针对本次会话中的第一个请求执行此操作。

该手册讨论了 Zend_Session::isStarted()Zend_Session::sessionExists() 方法。但似乎这两种方法都只适用于当前请求(这意味着如果我在应用程序中的某处使用 Zend_Session::start() ,它会返回 true )。

我的方法如下: 我尝试重写 Zend_Session::start() 来将统计数据插入到我的数据库表中。

// Somewhere in my bootstrap:
My_Session::start();

// This is my class (eased up)
class My_Session extends Zend_Session
{
    public static function start($options)
    {
        parent::start($options);

        if(/* Here I need the condition to test, if it was the initial session-starting... */)
        {
            $table = new Zend_Db_Table(array('name' => 'referer'));
            $row = $table->createRow();
            $row->url = $_SERVER['HTTP_REFERRER'];
            $row->ip = $_SERVER['REMOTE_ADDR'];
            // ... some columns ...
            $row->save();
        }
    }
}

有人有什么想法吗?

I've got a problem with Zend_Session. I need to know, if the Session for this user was initially started the first time or if it was just updated in the current request.

I need to know that for statistics. If the session was initialized (meaning the user visits my app for the first time) I want to store the referer of the request in some db-table. This of course I only want to do for the first request within this session.

The manual talks about the methods Zend_Session::isStarted() and Zend_Session::sessionExists(). But it seems that both methods only work for within the current request (meaning it returns true if I use Zend_Session::start() somewhere in my app).

My approach was the following:
I tried to override Zend_Session::start() to insert the statistic-data into my db-table.

// Somewhere in my bootstrap:
My_Session::start();

// This is my class (eased up)
class My_Session extends Zend_Session
{
    public static function start($options)
    {
        parent::start($options);

        if(/* Here I need the condition to test, if it was the initial session-starting... */)
        {
            $table = new Zend_Db_Table(array('name' => 'referer'));
            $row = $table->createRow();
            $row->url = $_SERVER['HTTP_REFERRER'];
            $row->ip = $_SERVER['REMOTE_ADDR'];
            // ... some columns ...
            $row->save();
        }
    }
}

Anybody has any idea?

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

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

发布评论

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

评论(1

情独悲 2024-11-13 18:46:26

我需要知道该用户的会话是第一次启动还是刚刚在当前请求中更新。

不是问题:

Zend_Session::start();
$my_logger = new Zend_Session_Namespace('my_logger');
if(isset($my_logger->has_already_visited_app) && $my_logger->has_already_visited_app) {
  // this is not the first request
} else {
  // this is the first request, do something here

  // make sure to add the following
  $my_logger->has_already_visited_app = true;
}

I need to know, if the Session for this user was initially started the first time or if it was just updated in the current request.

Not a problem:

Zend_Session::start();
$my_logger = new Zend_Session_Namespace('my_logger');
if(isset($my_logger->has_already_visited_app) && $my_logger->has_already_visited_app) {
  // this is not the first request
} else {
  // this is the first request, do something here

  // make sure to add the following
  $my_logger->has_already_visited_app = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文