在 PHP 中,要判断页面关闭,可以使用 register_shutdown_function() 函数。以下是一个示例代码:
<?php
function checkShutdown() {
if (!connection_aborted()) {
// 页面未关闭
echo "Page is still open";
} else {
// 页面已关闭
echo "Page has been closed";
}
}
register_shutdown_function("checkShutdown");
?>
在上面的代码中,我们定义了一个名为 checkShutdown 的函数,并使用 register_shutdown_function() 函数将其注册为脚本结束时的回调函数。在 checkShutdown 函数中,我们使用 connection_aborted() 函数检查页面连接是否已关闭。如果页面未关闭,我们将输出 "Page is still open",否则输出 "Page has been closed"。
注意:在使用 register_shutdown_function() 函数时,需要将其放置在页面的顶部,以确保脚本结束时回调函数能够被正确调用。