Monty
Monty

Reputation: 1322

Manually determine the last thing PHP does before it renders a page?

I'm trying to figure out the best way to unset a php session variable, after all pages have loaded and the page will be rendered to the browser. I have a page that includes & requires several pages to be rendered. I want to know if there is a built in php function that will tell php to do something right before the page is completed. Or what would the best way/practice to do this?

EDIT## Here's what I added...

function unsetHIST_ID(){unset($_SESSION['Hist_CID']);}
register_shutdown_function('unsetHIST_ID');

Upvotes: 0

Views: 60

Answers (3)

Baba
Baba

Reputation: 95121

Am not sure why you want this but if you want a function to be executed after script execution finishes or exit() is called then you should look at register_shutdown_function

Example

function shutdown() {
    echo "Am dead anyway";
}

register_shutdown_function('shutdown');

echo "<pre>";
echo "Hello am Alive",PHP_EOL;

Output

Hello am Alive
Am dead anyway <---------------This would always run last 

Upvotes: 1

LSerni
LSerni

Reputation: 57418

You need something like

register_shutdown_function ('my_atexit_function');

or maybe you can do this with

ob_start('content_handler');

The first function will be called immediately before exiting, which is not exactly what you asked for; and the second just before output, but that allows you to do something with an output that's already made static.

I'd go with register_shutdown_function though.

Upvotes: 1

Marc B
Marc B

Reputation: 360742

There's the .ini directive auto_append_file, which is parsed last by PHP if specified. It's basically treated as if you'd put require('somefile.php') as the very last command in a file yourself.

Upvotes: 1

Related Questions