每次我访问此IP时,此代码都会重复一个IP命中... 如果我以IP点击次数递增而不是IP重复访问
我该如何解决?有人可以指导吗?
$date = new \DateTime;
$check_if_exists = DB::table('visitors')
->where('ip',$_SERVER['REMOTE_ADDR'])->first();
$get_visit_day = DB::table('visitors')->select('created_at')
->where('ip', $_SERVER['REMOTE_ADDR'])->first();
$value = date_create($get_visit_day->created_at);
if(!$check_if_exists)
{
DB::table('visitors')->insert(array('ip' =>
$_SERVER['REMOTE_ADDR'], 'hits' => '1', 'created_at' => $date));
}else{
DB::table('visitors')->where('ip', $_SERVER['REMOTE_ADDR'])
->increment('hits')->insert('updated_at', $date);
// DB::table('visitors')->insert('updated_at', $date);
}
// $value = date_create($get_visit_day->created_at);
if ($check_if_exists && date_format($value, 'd') != date('d')) {
DB::table('visitors')->insert(array('ip' => $_SERVER['REMOTE_ADDR'], 'hits' => '1', 'created_at' => $date));
}[enter image description here][1]
答案 0 :(得分:0)
如果您想每天添加新记录,并为今天的每次点击增加观看次数,请使用唯一的ip尝试
$date = new \DateTime;
$check_if_exists = DB::table('visitors')
->where('ip',$_SERVER['REMOTE_ADDR'])->first();
$get_visit_day = DB::table('visitors')->select('created_at')
->where('ip', $_SERVER['REMOTE_ADDR'])->first();
$is_today_if_exists = DB::table('visitors')->whereDate('created_at',$date)
->where('ip', $_SERVER['REMOTE_ADDR'])->count();
if(!$check_if_exists)
{
DB::table('visitors')->insert(array('ip' =>
$_SERVER['REMOTE_ADDR'], 'hits' => '1', 'created_at' => $date));
}
elseif ($is_today_if_exists>0)
{
DB::table('visitors')->where('ip', $_SERVER['REMOTE_ADDR'])->whereDate('created_at',$date)
->increment('hits');
}
else
{
DB::table('visitors')->insert(array('ip' => $_SERVER['REMOTE_ADDR'], 'hits' => '1', 'created_at' => $date));
}