【发布时间】:2014-07-28 01:20:59
【问题描述】:
我想记录今天访问该网站的用户。为此,我必须在网站上的任何页面上处理用户访问。
什么是公共入口点(代码,在访问任何页面时执行)?
【问题讨论】:
-
index.php是 .htaccess 指向的所有内容
标签: php yii statistics entry-point
我想记录今天访问该网站的用户。为此,我必须在网站上的任何页面上处理用户访问。
什么是公共入口点(代码,在访问任何页面时执行)?
【问题讨论】:
index.php 是 .htaccess 指向的所有内容
标签: php yii statistics entry-point
我想如果你想记录哪些用户访问了网站,那么你应该在user 组件中实现这个功能(默认为CWebUser)。您可以扩展此 calss 并在用户组件的配置中指定它:
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
'class'=>'MyWebUser',
),
【讨论】:
public function init(){ parent::init(); //your code here } 并在那里调用记录方法。
您还可以创建类 BaseController 扩展 CController,并使用 init 方法。例如:
class BaseController extends CController
{
public function init()
{
$this->loggedUserId = Yii::app()->user->getId();
$this->isLogged = !empty($this->loggedUserId);
if ($this->isLogged) {
// some log actions
}
return parent::init();
}
}
【讨论】:
假设你在谈论 Yii 1.1
您可以在 onBeginRequest 和 onEndRequest 事件上附加您的逻辑:
示例(在适当的文件中,index.php/custom loader 脚本,简单)
Yii::app()->onBeginRequest = function(CEvent $event) { handle_event($event); };
或者在您的配置中将自定义行为附加到该事件:
'behaviors' => array(
'onbeginRequest' => array(
'class' => 'application.components.AnalyticsBehaviour',
)
)
并处理行为。
【讨论】: