gpt4 book ai didi

php - Laravel-访客计数器

转载 作者:行者123 更新时间:2023-11-29 15:50:19 25 4
gpt4 key购买 nike

每当我访问该 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]

最佳答案

如果您想每天添加新记录,并增加今天每次点击的观看次数,形成唯一的 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));
}

关于php - Laravel-访客计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56805420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com