Nico Amarilla

My personal notes on programming.

Use Apache Filter In Windows

In this tutorial we will use Apache’s mod_ext_filter module. To put simply, it allows you to easily process your content through an external program before it’s passed onto the user’s web browser.

System Info

  • Windows 7 – 64 bit
  • Apache 2.4
  • PHP 5.3


PHP should be runnable in the command line. To check if this is possible, open command prompt and enter: php –version. It should return information about php.

Configure Apache

  1. Open Apache’s main configuration file, httpd.conf. Mine is found in D:/webserver/apache24/conf/. Enable mod_ext_filter by uncommenting this line:
    LoadModule ext_filter_module modules/
  2. Next Add the ExtFilterDefine directives at the bottom of httpd.conf:
    ExtFilterDefine html_changer mode=output intype=text/html cmd="D:/webserver/phpfilter.cmd"

    The ExtFilterDefine directive defines the characteristics of an external filter, including the program to run and its arguments.

    • html_changer – is the filtername. This name can then be used in SetOutputFilter directives.
    • mode=output – tells Apache to process the response.
    • intype=text/html – specifies the internet media type (i.e., MIME type) of documents which should be filtered.
    • cmd=”D:/webserver/phpfilter.cmd” – cmd specifies the external command to run. Here it is pointing to a command line script that we will create below.
  3. Next add SetOutputFilter directive:
    SetOutputFilter html_changer

    This directive activates the html_filter which we have defined using ExtFilterDefine.

  4. Save and restart Apache

Create a Windows Batch File

  1. Create a file named phpfilter.cmd. Inside it put this code:
    @php "D:/webserver/filterscript.php"

    All it does is call php from the command line and run the script at D:/webserver/filterscript.php. The @ character means to suppress output.

Create the Filtering Script

  1. Create a php file and name it filterscript.php. Save it to where you point the windows batch file to. Mine is in D:/webserver/filterscript.php. Inside it place this code:
    $f = fopen( 'php://stdin', 'r' );
    while( $line = fgets( $f ) ) {
        $line = str_replace('</body>','<div style="background:#428bca; position:fixed; left:0; bottom:0; padding:2px 5px; font-size:12px; color:#fff">'.$_SERVER['REMOTE_ADDR'].'</div>', $line);
        echo $line;
    fclose( $f );

    As you can see this is a just a php script. What you don’t see often is the line:

    $f = fopen( 'php://stdin', 'r' );

    All it does is to read the input stream passed by Apache to PHP.

    while( $line = fgets( $f ) ) {

    This tells php to loop thru each line of input.

    $line = str_replace('</body>','<div style="background:#428bca; position:fixed; left:0; bottom:0; padding:2px 5px; font-size:12px; color:#fff">'.$_SERVER['REMOTE_ADDR'].'</div></body>', $line);

    This line is a rudimentary way of prepending a custom div to a body tag. The custom div has inline styles attached to it. It also shows the current IP address.

    echo $line;

    Returns the output line by line. The output is passed on to Apache to be served later on.

    fclose( $f );

    Closes the stream.

    That’s it, save the file.

  2. Finally test if the filter is working by going to http://localhost/ using your browser. You should see the inserted div at the bottom left:
    Inserted div at bottom left corner
    Inserted div at bottom left corner

Additional Resources

One Comment

  1. Raghav Singh

    hi !

    i want to show only username of the LDAP authenticated page on the html webpage getting displayed using apache http server.
    can this be done.



Leave a Reply

Your email address will not be published. Required fields are marked *