我已经保存了数据库中所需的数据,我需要在每个IP地址或用户ID上只保存一个URL,但每次重新加载页面时都会保存,所以我需要获取URL和IP地址但是每个页面只有一次,然后在30分钟后,当再次访问同一页面时,将保存相同的IP地址。
这是我的代码:
$ip = $_SERVER['REMOTE_ADDR']?:
($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']);
$page_view = new Pageview();
$user_view = User::find()->where(['id' => Yii::$app->user->id])->one();
$timedate = date('Y-m-d h:i:s');
$id = Yii::$app->request->url;
$view_count = Pageview::find()
->where(['user_ip'=> $ip])
->andWhere(['user_id'=> $user_view])
->andWhere(['page'=> $id])
->andWhere(['>=', 'date_time', date('Y-m-d h:i:s', strtotime('-30 minutes'))])
->count();
if($view_count == 0){
if(!Yii::$app->user->isGuest){
$page_view->user_id = $user_view['id'];
$page_view->user_ip = $ip;
$page_view->date_time = $timedate;
$page_view->page = $id;
$page_view->save(false);
}else{
$page_view->user_id = 0;
$page_view->user_ip = $ip;
$page_view->date_time = $timedate;
$page_view->page = $id;
$page_view->save(false);
}
}