Page 1 of 1

execute a function when the user leave the page

Posted: Tue Dec 02, 2003 6:23 pm
by mbleonardo
Anybody knows as to execute a function php when the user leave the page? (when it presses the X button to close)

In this case, I will like to drop any registry in a database.

Sorry for my bad english :wink:

Posted: Tue Dec 02, 2003 6:35 pm
by uberpolak
Can't be done, that's a client-side task, and PHP is server-side.

Posted: Tue Dec 02, 2003 6:44 pm
by mbleonardo
In ASP it gives to make this

Posted: Tue Dec 02, 2003 7:55 pm
by infolock
you can do this a lot easier with javascript..

something like this would work :

Code: Select all

<script language="JavaScript"> 
<!-- 
function onexit() 
{      
    document.location.href = 'mysite.php'; 
} 
//--> 
</script> 
</head> 
<body onUnload="onexit()">
you could throw some function calls, session calls, whatever inside that onexit function.

hope this helps.

Posted: Wed Dec 03, 2003 3:21 am
by mchaggis
mbleonardo wrote:In ASP it gives to make this
Only really works in IE tho, and I believe that these functions end up using JS at somepoint (could be wrong), it's just hidden from you.

Posted: Wed Dec 03, 2003 3:31 am
by Nay
infolock wrote:you can do this a lot easier with javascript..

something like this would work :

Code: Select all

<script language="JavaScript"> 
<!-- 
function onexit() 
{      
    document.location.href = 'mysite.php'; 
} 
//--> 
</script> 
</head> 
<body onUnload="onexit()">
you could throw some function calls, session calls, whatever inside that onexit function.

hope this helps.
mMm, infolock, wouldn't that only work when a user goes elsewhere in the browser? If he/she closes it, JS won't be able to execute........i think O_o.

-Nay

Posted: Wed Dec 03, 2003 4:35 am
by JayBird
Nay,

I think yyour right, you need to use onBeforeUnload.

This will execute whevener some closes the broswer, goes to another page within your site or clicks a link to another site.

I recently had to do this so that when a user closes the browser window, it automatically logged the user out. The ONLY way to do this is by useing frames.

Mark

Posted: Wed Dec 03, 2003 10:14 am
by mbleonardo
exists the library Php-gtk that I find that allows to make this, looked at:

<?php
dl('php_gtk.' . (strstr(PHP_OS, 'WIN') ? 'dll' : 'so'));
function saindo() { function when leave; }

$Janela = &new GtkWindow();
$Janela->connect('destroy', 'saindo'); //here
Gtk::main();
?>

Posted: Wed Dec 03, 2003 11:07 am
by Weirdan
PHP-GTK manual wrote: This extension will not allow you to display GTK+ applications in a web browser. It is intended for creating standalone GUI applications.