/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] detect php code finish??

Does anyone know of a way to detect when a php page is finished loading? I know I can use body onload however I would like it to be able to execute some php code before closing. Something like onexit(function()); or something.

Thanks

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@felgallApr 08.2008 — All the PHP runs before the page starts loading - you can't run any PHP after that unless you use Ajax.
Copy linkTweet thisAlerts:
@fastest963authorApr 08.2008 — so there is no way in PHP that I can detect when a php code finishes executing

however I can run an ajax script on body load (which would come when the php script finishes)??
Copy linkTweet thisAlerts:
@SheldonApr 08.2008 — You can use those two methods,


Or you can use a [url=http://php.net/register_shutdown_function]register_shutdown_function[/url]

This will run last after all processing has been completed.

[code=php]
<?php

// Running this function will call the function transform() after everything else is processed.
// Passing in any Varialbes you wish.

// The Caller
register_shutdown_function('transform', $var1, $var2);


//The function that will run
function transform($var1, $var2){
// This will never reutrn, So is only used for things like database updates or queries running the data throughput which is not need to display
return "Var1: {$var1}<br/>nVar2: {$var2}n";
}
?>[/code]
Copy linkTweet thisAlerts:
@SyCoApr 08.2008 — This will run last after all processing has been completed.[/QUOTE]

but as Felgall said, it's still before the page has even started loading. PHP is all server side. Ajax is the only way to execute PHP after a page has loaded without another page refresh.

Is use xajax as it's pretty easy way for PP developers to use ajax without having to learn reams of javascript. A simple onload(xajax_callphp()); can trigger the php function. It's pretty neat.

http://xajaxproject.org

http://us3.php.net/manual/en/function.register-shutdown-function.php
Copy linkTweet thisAlerts:
@fastest963authorApr 08.2008 — I get that, i wanted to figure out when the PHP script itself finished, i was just mentioning the ajax or an onload or something because I know that happens when the PHP script finishes executing
Copy linkTweet thisAlerts:
@fastest963authorApr 08.2008 — sorry, however it seems that I cannot edit my posts...

does anyone know of a way for javascript to kill a currently running (in the same window) php script? I know I can do a ajax load and set a session variable or something similar and then have the running code stop when that variable is set, however I was wondering if there was a javascript alternative? What I would like to be done is where a link stops a php script in an iframe and keeps the current output from the php script but stops it from running.

Thanks

Also, register_shutdown_function() worked perfectly, thanks!
Copy linkTweet thisAlerts:
@felgallApr 09.2008 — JavaScript can't kill a running PHP script. If it were run before the page loaded it is no longer running to be killed. If it were started by an Ajax call then you can have JavaScript either disregard the response back from the PHP or launch another PHP to undo the earlier one but you can't have it interact directly to kill it.
Copy linkTweet thisAlerts:
@fastest963authorApr 09.2008 — That's what I thought, but just wanted to make sure.

Thanks for all of the help!
×

Success!

Help @fastest963 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...