我不确定我是否已经完成想法,我想使用Laravel Framework在线创建用户页面,我想知道我用来将用户引入数据库的方法是否正确,如果时间超过了限制。
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
class Automatism {
public function handle($request, Closure $next) {
DB::table('users_online')->updateOrInsert(
['user-ip' => \Request::getClientIp(true)],
[
'user-viewing-url' => Route::getFacadeRoot()->current()->uri(),
'user-ip' => \Request::getClientIp(true),
'timestamps' => now()
]
);
DB::table('users_online')->where('timestamps', '<', 'now() - INTERVAL 5 MINUTE')->delete();
return $next($request);
}
}
然后在此代码旁边,我想对24小时之内访问过该网站的用户执行相同的操作。
答案 0 :(得分:0)
我制作了一个中间件来跟踪用户活动一次,这也许可以为您提供想法:
public function handle($request, Closure $next)
{
$this->request = $request;
if(Auth::check()) {
Cache::put('user-is-online-' . Auth::user()->id, true, Carbon::now()->addMinutes(5));
$this->storeDailyUserActivity();
}
return $next($request);
}
public function storeDailyUserActivity(){
$daily_active_user = [];
if(Cache::has('daily_active_user')){
$daily_active_user = Cache::get('daily_active_user');
if(!is_array($daily_active_user)){
Cache::forget('daily_active_user');
$this->storeDailyUserActivity();
return;
}
$daily_active_user['cache_old_date'] = $daily_active_user['cache_date'] ?? 'err_no_cache_date'; //debug only
}
$daily_active_user[Auth::user()->id] = Carbon::now();
$daily_active_user['cache_date'] = Carbon::now()->toTimeString() . ' - ' . $this->request->url(); //debug only
Cache::forever('daily_active_user', $daily_active_user);
}
isUserActiveInLastMinutes($minutes = 5)
)。在您确实需要将其保存到数据库的情况下,我会这样做:
last_activity
时间戳列(或在用户表和user_activity表之间建立oneToOne关系)