最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call function when PHP-page gets closed - Stack Overflow

programmeradmin3浏览0评论

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? :)

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? :)

Share Improve this question edited Jan 15, 2016 at 8:44 npit asked Jan 15, 2016 at 8:15 npitnpit 3095 silver badges22 bronze badges 2
  • 1 Just curious, any reason you are not using something like a last_activity time? is it a game? or...? – Clay Commented Jan 15, 2016 at 8:20
  • @ChrisBanks No, its a website with an filebrowser on it. I am using a last_activity_time... but I also wanna use the online/offline status :) – npit Commented Jan 15, 2016 at 8:23
Add a ment  | 

4 Answers 4

Reset to default 4

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

You cannot do it because php could be executed on backend, you can achieve desired result in next ways:

Preferable

If you need to do it in right way see @Philip Andersson answer. Also you could get an clear idea of how to use websockets in Create Live Group Tutorial.

Workaround

What you want to achieve can be done by applying next steps:

  1. create php file which would perform Users::isOffline(Users::getUserName()) while handling request from your browser
  2. Create javascript handler onbeforeunload, which would send AJAX request to newly created php file mentioned before.

Tricky moment: It's not trivial way to send Ajax Request from onbeforeunload handler. To do it use code provided below:

window.onbeforeunload = function() {
    var x = sendUserOfflineAjax();
    return x;
};

function sendUserOfflineAjax(){
        // Perform sending ajax request
}

What you try to achieve is not that easy using only php and javascript. Usually when you want to have a good online/offline status check you will have to use websockets.

There is a free subscription service called Pusher, i have used it many times and it's very easy to setup.

And also, here you can read how to set it up for online/offline: Show the connecting user login status using Websocket and Pusher

You can post user's information to another php file before page close.

Button would be like this:

<button type='button' data-userid='<?php echo Users::isOffline(Users::getUserName())?>'>click1</button>

You can post userid to a php file to update the status.

$('button').click(function(){
    var userid = $(this).attr('data-userid';
    $.ajax({
      type: "POST",
      url: "gooffline.php",
      data: {userid: userid},
      success: function(data)
      {
          alert("Come back soon!");
      }
   });
});

Php file would be like this:

$userid = $_POST["userid"];
$status = userLoggingOut($userid); //your db update function
if($status){
    echo json_encode(array("status"=>"success","message"=>"user info updated"));
}

Hope this solves.

发布评论

评论列表(0)

  1. 暂无评论