• Date: March 4, 2012
  • Author: Slawek Lukasiewicz
  • Comments: No Comments
  • Category: PHP

After looking at new functions introduced in PHP 5.4 we can found one called header_register_callback.

Using it, we can register callback which will be called before sending output.

Description from php.net

Registers a function that will be called when PHP starts sending output.
The callback is executed just after PHP prepares all headers to be sent, and before any other output is sent, creating a window to manipulate the outgoing headers before being sent.

Example: removing Server and X-Powered-By headers

function cleanHeaders() {
        foreach (array('X-Powered-By', 'Server') as $header) {
            header_remove($header);
        }
    }
 
    header_register_callback('cleanHeaders');

In modern frameworks we can easily achieve this, but sometimes we need to maintain legacy application, and this function can be really handy!

No Comments Yet

Leave a Reply →