brybam
brybam

Reputation: 5019

Is it possible to run a php function when someone closes a browser window?

For example, Lets say I have a flash swf game, and when users press connect they are assigned a random id that's then put into a mysql table. Then I have other users that connect and then can connect to these random ids. When users click disconnect i have the id taken out of the table that way when other people want to connect they dont get ids that arent active. is it possible that when a window is closed to have it run a php script? so i can get it to clear their user id?

Upvotes: 0

Views: 1167

Answers (2)

Jacob Marble
Jacob Marble

Reputation: 30122

You could do this with a bit of Javascript in the HTML (outside of Flash). This example uses jQuery's .unload():

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

Upvotes: 0

Pradeep Singh
Pradeep Singh

Reputation: 3634

use 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(); ">

Upvotes: 1

Related Questions