0

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

Rabab Zein
  • 15
  • 7

2 Answers2

3

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.

Community
  • 1
  • 1
marekful
  • 14,986
  • 6
  • 37
  • 59
0

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);
     } 

}
Rabab Zein
  • 15
  • 7
  • PHP is generally a bad choice for long-polling. IMO, marekfuls answer is better if you plan on having a lot of users online at the same time. – rdiz Oct 15 '15 at 14:53
  • yes I need it because the website will have alot of users online in the same time – Rabab Zein Oct 18 '15 at 09:38