Abstract
Hypertext Preprocessor (PHP) is a powerful server-side scripting language for the Apache Web server. PHP is popular for its ability to process database information and create dynamic Web pages. Server-side refers to the fact that PHP language statements, which are included directly in your Hypertext Markup Language (HTML), are processed by the Web server. Scripting language means that PHP is not compiled. Since the results of processing PHP language statements are standard HTML, PHP-generated Web pages display quickly. They are also compatible with most all Web browsers and platforms. PHP is for the open source Apache community as Net.Data is for the IBM community.
Restriction: IBM does not support PHP on the iSeries server. We provide these instructions for you to download a public domain open-source copy of a PHP engine to allow you to run PHP scripts on the iSeries server.
Contents
The following information was extracted from HTTP Server (powered by Apache): An Integrated Solution for IBM ~ iSeries Servers, SG24-6716, which is currently a draft redbook (Redpiece). This redbook has more details and provides you with step-by-step instructions.
Programming with PHP on the iSeries Server
The Hypertext Preprocessor language is a powerful, server-side scripting language for Web page creation. Scripting language means PHP requires no compilation, much like Perl or Rexx. Because PHP is a server-side language, you can include it directly in HTML, and it is recognized and processed by a Web server.
The first “P” in PHP is a remnant from the original acronym for Personalized Home Page. This was the term that PHP creator Rasmus Lerdorf used when he first used a set of Perl scripts to monitor access to his online resume. Since then, however, PHP has become the most popular optional module configured on Web servers. See the following Web sites for more information:
http://www.netcraft.com/survey
http://www.securityspace.com/s_survey/data/man.200304/apachemods.html
Note: If you want to know why this is so great, see the article “Programming with PHP on the iSeries” for iSeries Network by David Larson and Tim Massaro. With permission from iSeries Network, we include portions of the article in this IBM Redbook Hint & Tip. You can find this article on the Web at:
http://www.iseriesnetwork.com/resources/artarchive/index.cfm?fuseaction=viewarticle&CO_ContentID=15746&channel=art&PageView=Search
What is PHP?
PHP code can easily access database files and output HTML, resulting in non-static, up-to-date Web pages. It's a technique similar to JavaServer Pages (JSPs) or Common Gateway Interface (CGI) binary (often called CGI-BIN) programming. Also, PHP is an open-source project. Open-source code can be useful if you want to tweak the behavior of PHP. It's even more valuable because there are many open-source PHP applications and code samples available on the Web. This means you can get a new PHP Web project up and running quickly with little investment.
Hundreds of ready-made applications written in PHP are available as shareware, and many commercial products employ it. Until recently, PHP enjoyed a reputation for reliability and security.
The following figure shows the difference between standard static Web pages and dynamic Web pages using server-side PHP processing. In the first scenario on the left, a standard Uniform Resource Locator (URL) request arrives at the Web server asking for Web page http://www.example.com/index.html. The Web server sees this request and returns the HTML that is in the file somepage.html.
In the same figure with the second scenario on the right, the index.php file contains the special <?php tag that tells the Web server to process embedded PHP statements. After PHP processes those statements, it returns HTML statements to the Web server. Those statements are then sent back to the user included in the original HTML found in the file index.php. Because the PHP statements can run a command or Structured Query Language (SQL) statement, we say that the Web page is dynamically generated, as opposed to our previous static HTML page.
Why PHP?
In addition to the fact that PHP is so popular, why would you want to use it? There are several reasons:
- Easy to use: As we mentioned earlier, PHP is a scripting language included directly in HTML. This means that getting started is easy. There's no need to compile PHP programs or spend time learning tools that create PHP. You can simply insert statements and get quick turnaround as you make changes.
- Fully functional: The PHP language has built-in functions to access your favorite database. With PHP, your HTML pages can reflect current information by querying those databases, or you can use information about the user viewing your HTML Web page to customize the page specifically for that user. In addition to good relational database support, PHP is a complete language that includes powerful functions. You can create classes for object-oriented programming and use flat file or Lightweight Directory Access Protocol (LDAP) databases. Plus, it includes a spell checker, Extensible Markup Language (XML) functions, image generation functions, and more.
- Compatible and quick: Because PHP generates plain HTML, it's compatible with all Web browsers and refreshes quickly.
- Secure: Although PHP is open source, it's a secure environment. An advantage (over JavaScript, for example) is that all the Web clients see is pure HTML. Because the logic of the PHP program is never exposed to the client, security exposures are reduced.
- Open source: Yet another reason to use PHP is because it's an open-source project. This makes it easy to find examples and start using it quickly. The following Web sites are two examples of places where PHP scripts are shared:
http://www.sourceforge.net
http://www.phpresourceindex.com
The following code shows a simple "HelloWorld!" example:
<html>
<head><title>Standard HTML Page with PHP
HelloWorld</title>
<body>
<?PHP
print "Hello World";
print "<br> Generated with <b>PHP</b>";
?>
</body>
</html>
This is as simple as it gets. The file starts as a normal HTML file. We simply insert PHP statements following the <?PHP tag. The <?PHP tag is the signal to the HTML processor that PHP processing is necessary. The print statement is a PHP statement.
To make this example a little more useful, let's add the following statements to query the state of our Web server:
<?PHP
print "Hello World from System:" .
$HTTP_SERVER_VARS['HTTP_HOST'];
print phpinfo();
?>
The result of our PHP program is similar to what is shown in the following figure. This is a dynamic Web page that contains the name of our Web server and a table built by PHP with details about how PHP is configured on our Web server. This is accomplished by using one of several predefined PHP variables (for example HTTP_HOST) and the PHP function phpinfo.
Special Notices
This material has not been submitted to any formal IBM test and is published AS IS. It has not been the subject of rigorous review. IBM assumes no responsibility for its accuracy or completeness. The use of this information or the implementation of any of these techniques is a client responsibility and depends upon the client's ability to evaluate and integrate them into the client's operational environment.
