[英]Is it possible to delete an entire Webpage when the user navigates away?
如果用户浏览网页,是否可以执行php脚本?
我知道可以执行Javascript。
$(window).bind('beforeunload', function(){
return 'DataTest';
});
Cookies可能有效,但是我不确定侦听器如何跟踪过期的Cookie,然后删除正确的网页。
一个示例文件系统是这样的:
通常,要创建网站,php将写入data.txt,然后Python侦听器接受此更改,然后创建文件(用户[数字])。 您可能会想到,这些文件会随着时间的推移而堆积起来,因此需要将其删除。
http协议是无状态的,因此用户根本无法“导航”。 浏览器需要一个页面,服务器将返回该页面,并且通信将停止。 服务器没有可靠的方法来知道客户端将如何处理该页面。
免责声明:正如Fox所指出的那样,我不确定这是否适合您的情况。 我实际上赞成福克斯的回答。
但是,如果您绝对需要在用户离开页面后立即删除每个页面,请使用以下命令:
$(window).bind('beforeunload', function() {
$.ajax('yourscript.php?currentUser=0814HIFA9032RHBFAP3RU');
});
然后在yourscript.php中,输入以下内容:
<?php
// load your userId (for example, with $_SESSION, but do what you want here)
$actualUser = $_SESSION['userId'];
// checks if the requested id to delete fits your actual current user's id
if (isset($_GET['currentUser'] && $_GET['currentUser'] == $actualUser)
{
$user = $_GET['currentUser'];
$file = 'user'.$user.'.php';
unlink($file);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.