0

So im making a game lobby atm and want to run a .php script if a user closes or refreshes his browser.
My problem is, the .unload doesn't even fire, but it should, I basicly used the exact same thing in the wiki. What did I do wrong?

My index:

<script type="text/javascript">

jQuery(document).ready(function ($) {
    $(window).on('beforeunload', function() {
        return "Do you really want to leave this lobby?";
    });

    $(window).unload(function() {
        alert("wat");
        xmlhttp.open("POST","test_ajax.php",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("session=test");
    });
});

</script>

My test_ajax.php:

<?php

$db_name = "test";
$db_username = "root";
$db_password = "";
$db_host = "localhost";

mysql_connect($db_host, $db_username, $db_password) or die ("No connection possible!");
mysql_select_db($db_name) or die ("No database found!");

$v = "Testaccount";
$sql = mysql_query("UPDATE users SET Avatar=1 WHERE Username='$v'");

?>

Adrian Forsius
  • 1,437
  • 2
  • 19
  • 29
nn3112337
  • 459
  • 6
  • 21
  • 1
    Please, [don't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Jay Blanchard Sep 30 '14 at 13:58
  • 2
    AFAIK, there is no cross browser solution to do it. You could set ajax request to synch from onbeforeunload handler but don't expect it to work in all cases/browsers. – A. Wolff Sep 30 '14 at 14:01
  • So theres literally NO solution to see if a browser dcs/quits and then fire a function / script? – nn3112337 Sep 30 '14 at 14:03
  • Don't solely rely on the client telling you he is leaving. If I unplug my machine, you'll never know I left. There sure are solutions, but you need some fallbacks. – blex Sep 30 '14 at 14:04
  • 2
    If you don't really need to run it exactly as the user closes the browser, I would suggest implementing a heartbeat in ajax, and when you don't receive it in the span of a timeout, it is safe to assume the user has closed the window. – tfrascaroli Sep 30 '14 at 14:20

1 Answers1

1

IMO the only solution is to send some heartbeat signal every N seconds. If the heartbeat stops - user has closed the browser. There is no reliable JS event for browser closing. You can show a message with onbeforeunload event but you don't have enough time to send an ajax from that callback. If this callback does not return in a 1ms or so the page will just close. It is made this way because if a user enters a malicious page and wants to close it, the page won't be able to run some infinite loop in onbeforeunload callback to prevent the user from closing the page.

Dmitry
  • 6,716
  • 14
  • 37
  • 39
  • Hmm okey. thats where I sadly cant wrap my head around, im pretty new to Ajax n stuff, I dunno where I should even start. – nn3112337 Sep 30 '14 at 14:48