Hi,
I have used:
'user'=>array(
'class' =>'WebUser'
),
'session'=>array(
'class' => 'CDbHttpSession',
'sessionTableName' => 'sessions',
'autoCreateSessionTable' => false,
'connectionID'=>'db',
'timeout' => 24*24*60*60,
'gCProbability' => 100
),
to customize some functions. Now i would like to keep track of a specific sessions last activity by saving the current timestamp into the field lastActivity
in the sessions
table, on each pageload. How do I obtain that?
Thanks! 
class MyController extends CController
{
/**
* Initializes the controller.
*/
public function init()
{
// Register the event handler to keep track of the last activity
Yii::app()->attachEventHandler('onEndRequest', array($this, 'trackActivity'));
// Cascade
parent::init();
}
/**
* Function to track the last activity of the user
* @param CEvent $event
* @return void
*/
public function trackActivity(CEvent $event)
{
// Do the tracking here
}
}
Now just have all your controllers extend from this base controller.
You don’t have to do it this way. In fact, if you already have a lot of code and don’t want to change your controllers I recommend not doing it this way. The important part is, at some point, creating an event handler for the event you want. This handler could even be in some random helper class somewhere, and you could create the hook in your index.php file.
-- somewhere_else.php
class ActivityTracker
{
public static function trackLastActivity(CEvent $event)
{
// Stuff to track information here
}
}
-- index.php
...
$app = Yii::createWebApplication($config);
$app->attachEventHandler('onEndRequest', array('ActivityTracker', 'trackLastActivity'));
$app->run();
Whatever works.
Okay, thanks.
But this would force me to save a session twice, first in the database, second on end of the request. That must be bad practice?
My solution is just an event that will be processed every time a page execution ends properly. If you wanted, you could use the ‘onBeforeAction’ event. That would execute every time just before your action code was executed.
Are you storing all you data in the session table? If you are using a db session, say with CDbHttpSession, then you could hook an event in the session handler to handle this - if you wanted to do it in Yii.
HOWEVER, you can also just do it straight with the database table itself. I’m going to assume you are using MySQL and your table is using the MyISAM engine. Things may work a little differently if you are using InnoDB (they have different rules about time fields). You can declare your table, with the last activity field as follows:
CREATE TABLE IF NOT EXISTS `session` (
-- ... fields ...
`lastActivity` timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
-- ... key definitions ...
) ENGINE=MyISAM;
With this, lastActivity will default to the current timestamp when a new session record is created and any time the session record is updated it will update lastActivity to the time the update occurred. I think that might be the solution you are looking for.