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

javascript - How to detect if a user leaves a page in PHP - Stack Overflow

programmeradmin3浏览0评论

There is this function which i want to execute in case the user leaves a particular page. This function will basically change all of the data within a certain column in my database. So in case the user leaves this page, i want this function to be performed by the system. Is there a way to detect if the user already left the page.

Thanks!

There is this function which i want to execute in case the user leaves a particular page. This function will basically change all of the data within a certain column in my database. So in case the user leaves this page, i want this function to be performed by the system. Is there a way to detect if the user already left the page.

Thanks!

Share Improve this question edited Jan 12, 2017 at 9:59 Michał Perłakowski 92.5k30 gold badges163 silver badges186 bronze badges asked Jun 14, 2012 at 3:11 MikeMike 711 gold badge1 silver badge2 bronze badges 3
  • 2 Long story short: there is no reliable way. There are some javascript events that handle unload, but they can not be guaranteed. – Ayush Commented Jun 14, 2012 at 3:13
  • stackoverflow.com/questions/1889404/… – s_p Commented Jun 14, 2012 at 3:19
  • Possible duplicate of JavaScript, browsers, window close - send an AJAX request or run a script on window closing – Michał Perłakowski Commented Jan 12, 2017 at 9:58
Add a comment  | 

6 Answers 6

Reset to default 8

With 100% reliability, no, it's not possible. Since leaving a particular page is a client-side operation, you have 0 control over what the client does. You can register an onbeforeunload handler via Javascript and hope that the client browser supports it. But again, support for this isn't universal.

You can't do this in php but you could use the javascript function onbeforeunload, here an example

<script type="text/javascript">
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
  }
</script>

You can't do that using PHP since it's a server-side language. You will need to use JavaScript.

Since server-side php only runs the server script and then sends the result to the browser you can't do this with php alone. You could send an ajax request to your php script with the onunload event using javascript, and then run the php script in response to that request.

To cover the cases where the user leaves a particular page to navigate to another one inside the same website, you can try to achieve this using page IDs, store the ID of the "page_to_be_left" in a session var, and check if the current page ID matches the one stored in the session var. Your function could then be triggered if there is no match.

You need to use the javascript function onbeforeunload

发布评论

评论列表(0)

  1. 暂无评论