Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function when PHP-page gets closed

Is there a way to call a function when the php-page gets closed?
I am saving the online/offline status in my MySQL db.
With my function

Users::isOffline(Users::getUserName())

I'm writing "offline" into the db.
Now I want to call this function when someone is closing the page with the close button.
I've tried this:

<script language="JavaScript" type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
var offline= <?php echo Users::isOffline(Users::getUserName())?>;
}
</script>


But then, the function gets called when the php-page is loaded....
Any ideas? :)

like image 805
npit Avatar asked Apr 26 '26 18:04

npit


1 Answers

It is because PHP code doesn't run on client side - you need to use JS.

function confirmExit(){
//here you send request to your server
}

And on serverside you recieve request and mark user as offline

like image 59
Manasov Daniel Avatar answered Apr 29 '26 07:04

Manasov Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!