I need to detected when user close browser , to save record in data base ,
so how can I know when user close browser ? in laravel 5
I need to detected when user close browser , to save record in data base ,
so how can I know when user close browser ? in laravel 5
You can't by default. Laravel is a framework for server side scripting. And HTTP is a stateless protocol. The server only knows about the client when it sends a request. Having said that, you may implement a mechanism to regularly send "ping" requests from each connected client. The lack of these ping requests could indicate the end of a user serssion.
Finally I solve this problem and can check if connection failed or your close his browser or close his computer without logout from my website , to log him out from my website and record time of his logout
The solution :
Java script : setInterval(function(){
$.ajax({
url: '/home/outFromSystem' ,
type: 'POST',
data: {
},
success: function(result) {
console.log(result);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
}, 50000);
PHP : public function outFromSystem(){
ignore_user_abort(true);
for ($i = 0; $i < 10; $i++) {
flush();
ob_flush();
if (connection_aborted()) {
// your code
}
sleep(1);
}
}