cookies - Tracking non logged-in users in Yii/PHP -
i trying track actions of non-logged in users on site. aim store activity can add profile when create account.
i using behaviour below assign new users cookie , use cookie basis of "temp user" row in users table. way user can straight away start interacting api.
this seems work fine. however, seeing loads more "temp user" rows being created in db have visitors site - 2500 compared around 500 visits yesterday (according google analytics).
is there wrong behaviour below, or doing else wrong? there 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 enabled first:
check if cookies enabled
if we're correct, every time see user guest , not have cookie you're creating new temp user.
why not check see if cookie set first, if create temp user? end needing set 2 cookies: initial temp cookie check against, , 'dc_tempusername'
cookie.
you go far using browscap check against known bots:
https://github.com/browscap/browscap-php
http://browscap.org/
you'll need able define browscap in php.ini
Comments
Post a Comment