# 12. Web Application Development

Chapter Contents | Prev | Next
Keyword Search:
www.openlinksw.com
docs.openlinksw.com
Book Home
Contents
Preface
Web Application Development
The HTTP Server
Web Services ACL (Access Control List)
Virtuoso Server Pages (VSP)
Virtuoso Server Pages for XML (VSPX)
Writing ASP.Net Web Applications
ASMX Web Service Hosting
Blogging & Weblogs
Writing PHP Applications
Building the Virtuoso Server With PHP Extension PHP Extension Functions PHP Examples
Writing JSP Applications
Perl Hosting
Python Hosting
Ruby Hosting

## 12.8. Writing PHP Applications

The PHP server extension allows Virtuoso to execute PHP (v4) pages stored in the file system or in Virtuosos WebDAV repository. PHP pages run inside the Virtuoso process.

Figure: 12.8.1. The HTTP PHP handler

The VSE __http_handler_php() has been implemented so that the file extension '.php' is recognised by Virtuoso to switch between 'normal' mode and extension processing for PHP mode in the HTTP/WebDAV services.

PHP (recursive acronym for "PHP: Hypertext Preprocessor") is a widely-used Open Source general-purpose scripting language that is suited for Web-based development. Here is an example of a simple PHP page:

PHP introductory example

    <html>
        <head>
            <title>Example</title>
        </head>
        <body>

            <?php
            echo "Hi, I'm a PHP script!";
            ?>

        </body>
    </html>

Notice how this is similar to VSP script, and different from a script written in other languages like Perl or C -- instead of writing a program with lots of commands to output HTML, you write an HTML script with some embedded code to do something. The PHP code is enclosed in special start and end tags that allow you to jump into and out of "PHP mode".

See Also:

PHP Online Documentation

In-Process Data Access Client

### 12.8.2. Building the Virtuoso Server With PHP Extension

1. Firstly you need to have the PHP4 library installed with Zend and ODBC interface enabled. On UNIX-es this can be enabled by doing 'configure --enable-experimental-zts --with-iodbc'. On Windows the library must be downloaded from the php.net site.
2. Make sure that the iODBC library is installed if you are preparing to build on UNIX platform.
3. Make the bif_server_php executable in the bifsdk/examples/ directory. This same directory contains the source file for the bif_server_php executable: bif_server_php.cpp.

The Virtuoso distribution may already contain a binary executable of the PHP extension of the Virtuoso's Web Service. In which case you may skip the build process and start the server with the PHP extensions instead of starting the normal virtuoso server executable.

To start and test the Virtuoso server with the PHP extension do the following:

1. Make a simple file 'info.php' and place it in the HTTP root directory. The content of the info.php file should be the single line:

       <?phpinfo();?>

   This function produces an HTML page containing various PHP processor information.

2. Start the server. For Linux platform: bif_server_php -f, for Windows platform: bif_server_php-odbc-t.exe -f
3. Test the installation by entering the http://[host:port]/info.php as the URL of a browser of your choosing.

Note:

Windows users will need to install the php4ts.dll (from the php.net site) before running the PHP enabled Virtuoso server. This DLL must be in the system path when Virtuoso starts and can typically be placed in the %SYSTEMROOT% directory without any path modifications.

Note:

The PHP library used with Virtuoso must be version 4.3.1 or greater. From this version the PHP libary includes a serious PHP CGI vulnerability fix, without which the Virtuoso server will fail to start for security reasons.

### 12.8.3. PHP Extension Functions

The following functions have been added to the Virtuoso server in order to enable PHP processing.

PHP Server Handler
__http_handler_php (in file_name varchar, in params vector (opt1, value [,...]) or string session, in lines vector (opt, value[,...] ), [inout what any]);

This function will be detected and called automatically by the Virtuoso HTTP/WebDAV server when a request for a file with extension .php is made.

- The file_name argument will be the path to the file when the PHP page is stored on the file system or the actual content of the page when a WebDAV resource is processed.
- The content of request entity body will be stored in a string session and passed down to the __http_handler_php as the params argument.
- The HTTP request header will be passed as the lines argument.
- The what parameter is used for in two ways: to indicate whether the first argument is a path/file name to a file on the filesystem or the content of the required WebDAV resource. The HTTP/WebDAV server will return an array of two elements to this parameter.

PHP Processor
__http_php_str (in string varchar, in params vector (opt1, val1 [,...]) , varchar);

This function takes a string containing PHP code (page), and parameters supplied in the params array and returns the result from the PHP engine as a string. This can be useful for performing PHP transformations in PL or VSP code.

- the string is the source of the PHP page
- params is a string or array of strings containing parameter name and parameter value pairs. In the case of a single string it must contain form parameters in the application/x-www-form-urlencoded encoding. In case of an aref of strings it must contain the name and value for parameters. (Like the params argument in the VSPs).

### 12.8.4. PHP Examples

Examples of the php_str() usage

Unless the examples are shown as executed in the ISQL tool, this can be made also in the Virtuoso/PL code.

    SQL> select php_str ('<?php echo "Hello World"?>');
    callret
    VARCHAR
    _______________________________________________________________________________

    Hello World

    SQL> select php_str ('<?php print abs (-1);?>');
    callret
    VARCHAR
    _______________________________________________________________________________

    1

    SQL> set MACRO_SUBSTITUTION off;
    SQL> select php_str ('<?php echo $a?>', 'a=Hello+World');
    callret
    VARCHAR
    _______________________________________________________________________________

    Hello World

    SQL> select php_str ('<?php echo $a?>', vector ('a', 'Hello World'));
    callret
    VARCHAR
    _______________________________________________________________________________

    Hello World

    SQL> select php_str ('<?php echo "$a $b"?>', 'a=Hello+World&b=Hello+Again+World');
    callret
    VARCHAR
    _______________________________________________________________________________

    Hello World Hello Again World

    SQL> select php_str ('<?php $a=1; $b=2; $c=3; $d=$a+$b+$c; echo $d?>');
    callret
    VARCHAR
    _______________________________________________________________________________

    6

    SQL>  select php_str ('<?php $a=8; $b=4; $c=8; echo $a|$b&$c?>');
    callret
    VARCHAR
    _______________________________________________________________________________

    8

Examples of the php redirect page

This is test how the PHP processor can instruct the HTTP server to send a custom response and header to the user agent. The following page can stored on the file system or in the WebDAV repository (in which case execution rights must be enabled). Hitting that page will redirect user-agent to the index.html page in HTTP server root.

    <?php
      header ("HTTP/1.1 302 Found");
      header ("Location: /index.html");
    ?>

Previous Blogging & Weblogs | Chapter Contents | Next Writing JSP Applications