Page 1 of 2

PLEASE HELP - Update database on browser close??

Posted: Thu Nov 11, 2010 6:11 am
by ewannnn
Hi, I have been working on an application where when the user logs in, the status field in the database isset to 1 (online), and when logging off set to 0 (offline).
I was wondering if there is a way, using PHP or otherwise, of accomodating for when the user closes the browser manually?
I have been looking at other forums who say that it is possible to set a timer which automatically logs the user off after a set amount of time but I dont know how to actually do this or where to start; will the code go on every page of the site?
Thanks in advancefor any replies :)

Re: PLEASE HELP - Update database on browser close??

Posted: Thu Nov 11, 2010 6:22 am
by dheeraja
Use onunload event and call javascript, inside javascript use php and jst set what you want...

Re: PLEASE HELP - Update database on browser close??

Posted: Thu Nov 11, 2010 6:25 am
by ewannnn
Thanks :) do you know if there are any tutorials to do that, and will the code need to go on every page in the site? Thanks again :)

Re: PLEASE HELP - Update database on browser close??

Posted: Thu Nov 11, 2010 6:38 am
by dheeraja
you can just put your code to any file say xyz.php and can call to all files using require_once like <?php require_once("xyz.php");?>, well firstly tell me what you are using which shows the status of a user that he is login or not, i.e., you are using session or cookie...???

Re: PLEASE HELP - Update database on browser close??

Posted: Thu Nov 11, 2010 6:52 am
by ewannnn
Hi again dheeraja, would it be something like:

In header:
<script language="JavaScript" type="text/javascript">
function reset_login(){
<?php
set value to off etc...
?>
}
</script>

...then in the html body tag:

<body onunload event="reset_login();">

Thanks again in advance :)

Re: PLEASE HELP - Update database on browser close??

Posted: Thu Nov 11, 2010 6:59 am
by dheeraja
Yup you are right, but jst correct the syntax of calling event, it will be,
<body onunload="reset_login();">

and remember one thing put your JS code to head section...

Re: PLEASE HELP - Update database on browser close??

Posted: Thu Nov 11, 2010 10:18 am
by ewannnn
Fantastic thanks so much...will this work if the user actually closes the browser? Also do you know if it is possible to call two functions from inside the body tag? I mean a <body onload> and a <body onunload>?
Again thanks so much, Id never even heard of onunload event :)

Re: PLEASE HELP - Update database on browser close??

Posted: Thu Nov 11, 2010 10:26 am
by s.dot
You might want to use onbeforeunload(). Actually I believe different browsers do different things with onunload() and onbeforeunload() (whether it be implementing them or their behavior on refresh, browser back, browser forward, etc).

You may want to look at a more elegant solution for the problem you are having.

And yes, you can have them both in the body tag.

Re: PLEASE HELP - Update database on browser close??

Posted: Thu Nov 11, 2010 11:28 am
by ewannnn
Thanks for the advice :) Here's where i'm up to now...I have put the following code into the page that displays the user status:

<body onunload=<?php require_once("reset_login.php");?>>

...and this is the reset_login.php file:

<?php
include("connect.php");
mysql_query("UPDATE users SET status='Offline' where alias='".($_SESSION['alias'])."';");
?>

As soon as the page loads, the user status is updated to offline, as if it is executing the query when the body loads instead of unload.

I have also tried putting the php in a JavaScript function in the page header then calling it on body onunload but it does the same again, executes the query on body load.

Thanks also s.dot I have tried onbeforeunload but still does the same.
If you have an idea where i am going wrong, PLEASE HELP!! I am pulling my hair out! :)

Re: PLEASE HELP - Update database on browser close??

Posted: Thu Nov 11, 2010 11:45 am
by McInfo
When you call a PHP page in your browser, a request is sent to the server. The server finds the requested PHP file, reads the script, and performs all of the actions defined therein.

In the process, all of the PHP code is stripped from the script (in memory, of course; not the file itself) and replaced by whatever output is generated. In your case, the output is HTML and JavaScript. The output is returned in a response to the browser. The browser (and therefore, JavaScript) never sees the PHP code.

For JavaScript to influence something on the server, it must trigger another request by using AJAX, changing the window location, changing the src attribute of an iframe or img, or similar method.

Re: PLEASE HELP - Update database on browser close??

Posted: Thu Nov 11, 2010 12:26 pm
by ewannnn
McInfo wrote:For JavaScript to influence something on the server, it must trigger another request by using AJAX, changing the window location, changing the src attribute of an iframe or img, or similar method.
Thanks, so will the code in the body tag not work no matter what then? If not, is there a simpler mathod than AJAX? Thanks again :)
s.dot wrote:You might want to use onbeforeunload(). Actually I believe different browsers do different things with onunload() and onbeforeunload() (whether it be implementing them or their behavior on refresh, browser back, browser forward, etc).

You may want to look at a more elegant solution for the problem you are having.

And yes, you can have them both in the body tag.
Thanks, I cant get the onunload to work anyway, it sets it to offline onload for some reason(?). Do you have any ideas for another way round it? Thanks again :)

Re: PLEASE HELP - Update database on browser close??

Posted: Thu Nov 11, 2010 1:19 pm
by McInfo
Super-simple example:

page.php

Code: Select all

<html>
    <head>
        <title>Logger</title>
        <script type="text/javascript" src="page.js"></script>
    </head>
    <body>
        <h2>Log</h2>
        <pre><?php include 'log.txt'; ?></pre>
        <p><a href="">Refresh</a></p>
    </body>
</html>
page.js

Code: Select all

window.onbeforeunload = function () {
    document.createElement('img').src = 'log.php';
}
log.php

Code: Select all

<?php
file_put_contents('log.txt', date('Y-m-d H:i:s') . "\n", FILE_APPEND);
log.txt
(empty)

Re: PLEASE HELP - Update database on browser close??

Posted: Thu Nov 11, 2010 2:56 pm
by ewannnn
Thanks very, very much McInfo :) ahhh, so if i put my query in log.php:

Code: Select all

<?php
include("connect.php");
mysql_query("UPDATE users SET status='Offline' where alias='".($_SESSION['alias'])."';"); 
?>
...and page.php in the header of my page which I want to display the user status on...
...and a seperate JavaScript file with the page.js code on, this should work then, thanks so much i will give it a try tomorrow :)

Re: PLEASE HELP - Update database on browser close??

Posted: Fri Nov 12, 2010 5:01 am
by ewannnn
It tried that and it doesn't work... :(

Re: PLEASE HELP - Update database on browser close??

Posted: Fri Nov 12, 2010 11:43 am
by McInfo
"Doesn't work" doesn't help. A description of what you expect and what you get instead is more helpful.

$_SESSION will not retain any values between requests unless session_start() has been called.