You are currently on IBM Systems Media’s archival website. Click here to view our new website.

IBM i > TIPS & TECHNIQUES > APPLICATION DEVELOPMENT

Beginning PHP Programming




PHP extracts the variables from this URL and places them in the $_GET array. When the PHP code runs, your Web pages title is "Programming PHP" and the statement "Programming PHP is fun!" appears on the page. The "%20" in the URL represents a space; "%21" is the encoding for an exclamation point.

 

The syntax of "if" statements (such as the one in the fourth line of the code) is similar to that found in C, Java or Perl. The code in the brackets following "if" runs if the statement evaluates to true. This line also contains a function called "isset()", which determines if a value is assigned to the variable given as a parameter. PHP finds the value of $_GET['printme'] to be "true," so isset returns true, and the code inside the "if" statement is executed. In this case, the fifth line of code prints the contents of the $_GET['contents'] variable inside some

 tags.

 

The results are quite different if you access the same Web page, but without the "printme" variable set:

http://mywebserver/webpage.php

Here, the $_GET['printme'] variable isnt set, so the "if" statement evaluates to false, and instead the code in the "else" portion runs:

for ($i = 0; $i < 10; $i++) {
  echo ($i . "
\n");
}< /SPAN >

The syntax for "for" is also similar to the languages mentioned previously. In this case, a counter, "$i", is initialized to "0." Each time the loop executes, the counter increments by one-this continues while $i is less than 10. So the code simply outputs the numbers one through nine on separate lines.

Script Samples
PHP offers a rich collection of functions, and its API set is also one of the most well-documented I've seen. As noted in part one, the
PHP Web site offers a wealth of information on strings, databases, numbers, variable types, arrays, regular expressions and other PHP function. If you want to do something with a certain variable, the function is probably already built into PHP.

Consider these two sample PHP scripts, which use the API.

Basic WRKACTJOB Script-Code Sample 1 contains code that duplicates the output of the Work With Active Jobs (WRKACTJOB) command in your browser. This simple script grabs the output from WRKACTJOB and spits it back onto your browser window.

The only new concept here is the back-tick (`) operator, which runs a system command and returns the output to a string. In Code Sample 1, the $output string gathers the output from WRKACTJOB.

(Note: Because PHP is compiled in OS/400* PASE or AIX*, system commands must be run as youd run them in the OS/400 PASE environment. So in this case, you must enter "system WRKACTJOB" from the command line. Commands available directly in OS/400 PASE, such as ls, neednt be prefaced with "system.")

The output from Code Sample 1 should resemble Figure 1. This script is enhanced in the next section.

Basic Job Log Viewer Script-Code Sample 2 offers another example that allows users to view job logs over the Web. Users can select the job log information they want to view online by either entering the necessary information (job name, user name and job number) or by selecting a job from the WRKACTJOB list. No modification of the script is necessary.

The output from Code Sample 2 should resemble Figure 2, assuming you provide the script a valid job name, number and user.

This job-log viewer code isnt much more complicated than the WRKACTJOB script. The main difference is that now you use functions to group code by purpose and check user input. At this point though, user input is only checked to see that an entry is made. There's no attempt to verify that any of the information is correct, or that it isnt malicious. (Note: To use this script externally, you'd need to add code to verify user input. At minimum, I'd add checks to make sure that the user and job names contain fewer than 10 characters, are alphanumeric and that the job number is within a certain numerical range. This isn't difficult to do in PHP. Without these minimal changes, I wouldn't put this code example as-is on an external Web site.)

Enhancing the WRKACTJOB and Job Log View Scripts-Code Sample 1 and Code Sample 2 are useful, but, as you may have noticed, they require you to manually enter the job information. The good news, though, is you can automate this process by integrating these code samples. Parse each line of the WRKACTJOB script and locate the job name, number and user. Then create a link to Code Sample 2 with the variables filled out. This allows the new file to produce View Job Log links where appropriate (see Code Sample 3). Notice the page breaks and extra headers present in Code Sample 1 are now gone. In fact, the first nine lines of WRKACTJOB are gone, along with any headers and page breaks after what is now the first subsystem. After that, the only output should be jobs or subsystems. The new output should resemble Figure 3.

Working with PHP
PHP is fairly easy to implement, and, I believe, can simplify many iSeries programming and administrative tasks. Now that you have a taste of what PHP can do, you're ready to make it work for you.

Let's look at some other things you can do just with the two examples covered here. You can:

  • Alternate colors on wrkactjob.php lines to make reading easier
  • Remove unneeded/unwanted information from the WRKACTJOB output
  • View associated log files (we do this for our OneWorld jde.log and jdedebug.log files)
  • View WRKACTJOB output by subsystem-this would require only a slight change to allow users to specify the subsystem(s) they wish to view
  • Highlight or only display certain lines based on its contents (e.g., highlight every job belonging to a certain user, or in a certain job status)
  • Automatically jump to or highlight errors in a job log
  • Request that a job be ended

You can also create PHP scripts that allow you to:

  • View spool files over the Web
  • Start or stop subsystems
  • View or edit database table content

Hopefully by now you're considering implementing PHP on your iSeries system. For details, join my PHP mailing list.

 

 

David Stockton is a programmer with J.D. Edwards World Solutions Company. He can be reached at his Web site davidstockton.com.



Advertisement

Advertisement

2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

A PPP Monitor Utility

Here's a program developed to programmatically obtain the status of a PPP connection

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters