1

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?

Fidi
  • 5,754
  • 1
  • 18
  • 25
  • 1
    Check if the user is already in the DB? If not add it. edit: Ahh, ur only storing url and ip i see :) – Wouter van Tilburg May 13 '11 at 19:43
  • Yeah I had this idea, too. But this way I would have to save any unique-criteria identifying the user in this table. I think the ip is not reliable so the session-id itself might be. But in fact I don't really want to store the session-ids in my stats-table. So I really would like to find another approach. If it's absolutely not possible, I will of course your suggested way! Thanks. – Fidi May 13 '11 at 19:46

1 Answers1

2

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;
}
chelmertz
  • 20,399
  • 5
  • 40
  • 46
  • I also thought about something like that. I guess, that's the only way to do something like that. I'm also thinking about approach with overriding `Zend_Session_SaveHandler_DbTable`. When it does an insert-query to store the session initially, also store it in the statistics-table. But quick'n'dirty I guess I will use your approach. – Fidi May 13 '11 at 20:22
  • @faileN: I think this is the way to go if you want to rely on sessions. Perhaps you should rely on cookies instead. (And, to be frank, I think you're focusing more on the technical solution before defining the problem :)) About `Zend_Session_SaveHandler_DbTable`: If you store all sessions in your database, you're mixing concerns: statistics vs session management. That could be problematic in the future since you introduce coupling between two unrelated topics. Follow up question: are you trying to mimic the behaviour of something like Google Analytics or Piwik, or something else? – chelmertz May 13 '11 at 23:01
  • Well what means "mimic Google Analytics..". Let's say "in some way", but much easier. I just want to know, how many users where visiting a page and where they came from. Thanks anyway, I implemented your solution, using a key in the session to determine, if it was the first visit. – Fidi May 15 '11 at 19:44
  • About "mimicking Google", I meant their feature of tracking unique visitors - sorry for being unclear. Anyway, I hope that works out for you! – chelmertz May 15 '11 at 21:27