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? :)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With