public function hitsCounter($module, $moduleid = null) { $ip = $_SERVER["REMOTE_ADDR"]; $agent = $_SERVER["HTTP_USER_AGENT"]; session_start(); $session = \Yii::$app->session->getId(); $country = $this->visitor_country(); if (!in_array($_SERVER['HTTP_USER_AGENT'], array('facebookexternalhit/1.1 (+https://www.facebook.com/externalhit_uatext.php)', 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)', 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', 'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)'))) { $query = Sitecounter::find()->where(['module' => $module]); if ($moduleid) { $query->andWhere(['module_id' => $moduleid]); } $query->andWhere(['session_id' => $session]); $chk = $query->count(); if ($chk == 0) { $model = new Sitecounter(); $model->module = $module; $model->module_id = $moduleid; $model->ip_address = $ip; $model->session_id = $session; $model->user_agent = $agent; $model->country = $country; $model->datetime = date('Y-m-d H:i:s'); if (!$model->save()) { print_r($model->getErrors()); exit; } } } }