-3

I would like to detect when the page closes and after 10 seconds, use php to call a url. Since php is server side, I imagine that unlike JS, it will call the url regardless of whether the page is active or not.

URL to call: http://postback.com/?id=xx

Darren
  • 13,050
  • 4
  • 41
  • 79
Meteor
  • 55
  • 1
  • 6

2 Answers2

0

After you close your page you can't do anything related with user.

Use Cron job together with ajax calls.

Ajax sends to server if it's still online. Save timestamp in database along with some unique ID to identify browser.

Run Cron Job every second or two. Check if timestamp in database is older than 10 seconds. If so, use cUrl to call your url

Pseud code:

[js]
setInterval(
    function () {
        $.post('keepAlive.php?id='+$('#uid').val()),
    }
    4000
);

[PHP | keepAlive]

$db->insertOrUpdate($_GET['id'], time());

[PHP | cron]

$alive = $db->query('SELECT * FROM alive WHERE `time` < '.time());

foreach ($alive as $data) {
    $this->sendCurl($data);
}
Justinas
  • 41,402
  • 5
  • 66
  • 96
0

There is no 'close' event in browsers, probably for security reasons.

This post offers to use a child window to control the closing process. This way, in the parent window put the following code:

var win = window.open('http://www.google.com','google','width=800,height=600,status=0,toolbar=0');   
var timer = setInterval(function() {   
    if(win.closed) {
        clearInterval(timer);
        setTimeout (function () {$.get( "http://postback.com/?id=xx" );}, 10000); 
    }  
}, 1000);

This code has not been tested by me.

Community
  • 1
  • 1
guysigner
  • 2,822
  • 1
  • 19
  • 23