[英]Tracking non logged-in users in Yii/PHP
I am trying to track the actions of all non-logged in users on my site. 我正在尝试跟踪我网站上所有未登录用户的操作。 The aim is to store this activity so that I can add it to their profile when they do create an account.
目的是存储此活动,以便我可以在创建帐户时将其添加到其个人资料中。
I am using the Behaviour below to assign new users a cookie and use that cookie as the basis of a "temp user" row in my Users table. 我使用下面的行为为新用户分配一个cookie,并使用该cookie作为我的Users表中“临时用户”行的基础。 This way a user can straight away start interacting with my API.
这样,用户可以立即开始与我的API进行交互。
This seems to work fine. 这似乎工作正常。 However, I am seeing loads more "temp user" rows being created in my DB than I have visitors to the site - about 2500 compared with around 500 visits yesterday (according to Google Analytics).
但是,我发现在我的数据库中创建了更多的“临时用户”行,而不是我访问该网站的访问者数量 - 大约为2500,而昨天约为500次访问(根据谷歌分析)。
Is there anything wrong with the behaviour below, or am I doing something else wrong? 下面的行为有什么问题,或者我做错了什么? Is there a better way?
有没有更好的办法?
<?php
class ApplicationBehavior extends CBehavior
{
private $_owner;
public function events()
{
return array(
'onBeginRequest' => 'setCookies'
);
}
public function setCookies()
{
$owner = $this->getOwner();
if ($owner->user->getIsGuest() && !isset(Yii::app()->request->cookies['dc_tempusername'])):
$tempusername = genRandomString(20);
$tempuser = new User();
$tempuser->username = $tempusername;
$tempuser->email = "noemailyet@tempuser.com";
if (isset(Yii::app()->request->cookies['dc_tempusername'])) {
$tempuser->name = Yii::app()->request->cookies['dc_tempusername']->value;
} else {
$tempuser->name = "CookieBasedTempuser";
}
$tempuser->points = 1;
$tempuser->firstip = $_SERVER['REMOTE_ADDR'];
if ($tempuser->validate()) {
Yii::app()->request->cookies['dc_tempusername'] = new CHttpCookie('dc_tempusername', $tempusername);
$cookie = new CHttpCookie('dc_tempusername', $tempusername);
$cookie->expire = time() + 60 * 60 * 24 * 180;
Yii::app()->request->cookies['dc_tempusername'] = $cookie;
$tempuser->save();
} else {
echo CHtml::errorSummary($tempuser);
}
endif;
}
}
?>
Check if cookies are enabled first: 检查cookie是否先启用:
Check if cookies are enabled 检查cookie是否已启用
If we're correct, every time you see that the user is a guest and does not have a cookie then you're creating a new temp user. 如果我们是正确的,那么每当您看到用户是访客并且没有cookie时,您就会创建一个新的临时用户。
Why not check to see if a cookie is set first, if so then create the temp user? 为什么不检查是否首先设置cookie,如果是,那么创建临时用户? You would end up needing to set 2 cookies: initial temp cookie to check against, and then your
'dc_tempusername'
cookie. 您最终需要设置2个cookie:要检查的初始临时cookie,然后是
'dc_tempusername'
。
You could even go as far as using Browscap to check against known bots: 您甚至可以使用Browscap来检查已知的机器人:
https://github.com/browscap/browscap-php https://github.com/browscap/browscap-php
http://browscap.org/ http://browscap.org/
You'll need to be able to define browscap in your php.ini 您需要能够在php.ini中定义browscap
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.