execute a function when the user leave the page

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

Post Reply
mbleonardo
Forum Newbie
Posts: 3
Joined: Tue Dec 02, 2003 6:23 pm

execute a function when the user leave the page

Post 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:
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post by uberpolak »

Can't be done, that's a client-side task, and PHP is server-side.
mbleonardo
Forum Newbie
Posts: 3
Joined: Tue Dec 02, 2003 6:23 pm

Post by mbleonardo »

In ASP it gives to make this
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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.
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Post 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.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
mbleonardo
Forum Newbie
Posts: 3
Joined: Tue Dec 02, 2003 6:23 pm

Post 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();
?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

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