I'm developing a website in PHP, using Eloquent for database, and I'm having a certain problem of slowing down the user time to log in to the database.
What I mean is ... The user is taking almost 3 seconds to get logged in, and since I'm a bit of a perfectionist, I'm trying to improve it.
The fact is, when the user logs in, I write an insert in the database with the login information, (date / time, system used, location and etc ... so that the user have control of suspected irregular activities in your registry.)
I have noticed that the delay is because of this Select (to log in) and then Insert (for the login history) that is performed, that is, the select I know that is necessary and I have already tested, it is very well optimized ...
But the insert, I'm trying to perform asynchronously, so that PHP commands and go ahead with the command in the background.
Is there any solution in php or laravel / eloquent itself for this?
Edit: Here is the Login function in question:
public function login(){
$login = Login::select('idUser', 'userUrl', 'trueURL', 'firstName', 'lastName', 'email', 'password_hash')->where('email', '=', $_POST['email'])->first();
if($login){
if(password_verify($_POST['password'], $login->password_hash)){
session_start();
$_SESSION['user_data'] = ['idUser' => $login->idUser,
'trueURL' => $login->trueURL,
'userUrl' => $login->userUrl,
'firstName' => $login->firstName,
'lastName' => $login->lastName,
'email' => $login->email];
$now = date('Y/m/d h:i:s', time());
$browser = get_browser(null, true);
$guest_ipAddress = $_SERVER['REMOTE_ADDR'];
$ipDetails = json_decode(file_get_contents("http://ipinfo.io/{$guest_ipAddress}/json"));
$inputs = [ 'id_user' => $login->idUser,
'public_ip' => $guest_ipAddress,
'timestamp' => $now,
'device_platform' => $browser["platform"],
'device_parent' => $browser["parent"],
'device_type' => $browser["device_type"],
'country' => $ipDetails->country,
'region' => $ipDetails->region,
'city' => $ipDetails->city
];
$registerLoginHistory = RegisterUserLogin::create($inputs);
JSON(array('status' => true));
}else{
JSON(array('status' => false));
}
}else{
JSON(array('status' => false));
}
}