有人关闭浏览器窗口时是否可以运行php函数?

时间:2011-01-27 06:17:48

标签: php mysql flash actionscript-3 browser

例如,让我们说我有一个Flash swf游戏,当用户按下连接时,他们会被分配一个随机ID,然后将其放入mysql表。然后我有其他用户连接,然后可以连接到这些随机ID。当用户点击断开连接时,当其他人想要连接时,我会从表中取出ID,但他们不会得到不活跃的ID。有可能当一个窗口关闭,让它运行PHP脚本?所以我可以让它清除他们的用户ID?

2 个答案:

答案 0 :(得分:1)

使用onbeforeunload     

var clicked = false;

document.onclick = function()
{
//alert(event.srcElement.tagName);
    if(event.srcElement.tagName)
        clicked=true;
}
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {


  } else if (window.sidebar) {
  clicked= true;

  } else {
    if(window.opera && window.print) clicked= false;
  }
</script>

<script>
function doUnload()
{

  urlstring = "http://www.google.com/popup.php";// add popup url
            window.open(urlstring,'mywin',"height=400px,width=500px,status=no,toolbar=no");

}



</script>


 <body onbeforeunload="if(!clicked) doUnload(); ">

答案 1 :(得分:0)

您可以在HTML(Flash之外)中使用一些Javascript来完成此操作。此示例使用jQuery's .unload()

<script type="text/javascript">
$(window).unload(function() {
    $.ajax({ url: "clear_user_id.php"});
});
</script>