PLEASE HELP - Update database on browser close??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

ewannnn
Forum Newbie
Posts: 13
Joined: Thu Nov 04, 2010 9:46 am

PLEASE HELP - Update database on browser close??

Post 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 :)
dheeraja
Forum Commoner
Posts: 36
Joined: Tue Nov 09, 2010 11:03 pm

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

Post by dheeraja »

Use onunload event and call javascript, inside javascript use php and jst set what you want...
ewannnn
Forum Newbie
Posts: 13
Joined: Thu Nov 04, 2010 9:46 am

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

Post 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 :)
dheeraja
Forum Commoner
Posts: 36
Joined: Tue Nov 09, 2010 11:03 pm

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

Post 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...???
ewannnn
Forum Newbie
Posts: 13
Joined: Thu Nov 04, 2010 9:46 am

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

Post 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 :)
dheeraja
Forum Commoner
Posts: 36
Joined: Tue Nov 09, 2010 11:03 pm

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

Post 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...
ewannnn
Forum Newbie
Posts: 13
Joined: Thu Nov 04, 2010 9:46 am

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

Post 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 :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
ewannnn
Forum Newbie
Posts: 13
Joined: Thu Nov 04, 2010 9:46 am

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

Post 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! :)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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.
ewannnn
Forum Newbie
Posts: 13
Joined: Thu Nov 04, 2010 9:46 am

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

Post 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 :)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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)
ewannnn
Forum Newbie
Posts: 13
Joined: Thu Nov 04, 2010 9:46 am

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

Post 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 :)
ewannnn
Forum Newbie
Posts: 13
Joined: Thu Nov 04, 2010 9:46 am

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

Post by ewannnn »

It tried that and it doesn't work... :(
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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.
Post Reply