Lionpath

Apache-PHP-Filter and Java Documentation

This article describes an usage example of the PHP filter module within the Apache webserver in combination with Java or any other language.

Introduction

Using PHP as a filter enables a seamless integration in your web application regardless in which language it is written.
You could easily add some sugar candy on your website, without reengineering your existing application.

Configuration

In this chapter the specific configuration items of the Apache webserver (version 2 release) and the PHP are described.
Most important switch is the PHP setting:
--with-apxs2filter=/usr/local/apache2/bin/apxs
This setting should point to your apxs file of your Apache webserver, the above setting is pointing to the default location on Linux environments, when the Apache webserver has been compiled from sources. When you are using a different location you have to alter the directory path.

My complete configuration of the PHP module looks like this:

./configure  --with-apxs2filter=/usr/local/apache2/bin/apxs --with-libdir=lib64 --enable-mbstring --with-mysql --with-gd --with-curl --enable-bcmath

However, only the with-apxs2filter setting is important for the functionality.

After compiling and installing the PHP module, let us take a look at the configuration of the Apache webserver:

Adding the php module:

LoadModule php5_module        modules/libphp5.so

Now we will enable a PHP filter for a certain location /special:

<Location "/special">
  FilterProvider PHPFILTER PHP resp=PHPFILTER "*"
  FilterTrace PHPFILTER 0
  FilterChain PHPFILTER
  Header unset PHPFILTER
  Header unset X-Powered-By
</Location>

This enables a filter called PHPFILTER and assigns it with PHP the filter provider. The filter provider PHP is part of the compiled PHP module, as given by the -with-apxs2filter setting.

The filter will be triggered on the response, if a response header variable setting of PHPFILTER is given.
With the FilterTrace option you could enable debugging information and the FilterChain setting enables complex chains of filters. The Header directive is optional and removes the header entry from the response, so that nothing is visible to the client.

Usage

The usage from Java or any other language is quite simple

apache php filter

Your code simply writes PHP source code directly into the output, e.g.:

out.println("<?php echo "This is PHP:"; phpinfo(); ?>");
response.addHeader("PHPFILTER","ON"); 

The code will be transformed into the following HTML output:

<?php echo "This is PHP:"; phpinfo(); ?>

Which becomes the input for the PHP filter module, which in turn evaluates the script and shows a line of This is PHP: followed by the PHP info settings generated from the phpinfo() function.

In a real world application you might pass parameters to the PHP code and do something more meaningful. But by now you should know how it works.

If you have any trouble or questions please drop me an email to info at lionpath.com


Table of Contents

Quick-Links

 
.