SDSU CS 580 Client-Server Programming
Fall Semester, 2000
FastCGI and PHP
Previous    Lecture Notes Index    Next    
© 2000, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 25-Oct-00

Contents of Doc 16, FastCGI and PHP


References

Integrating FastCGI with Java, http://www.fastcgi.com/devkit/doc/fcgi-java.htm


Philip and Alex's Guide to Web Publishing http://www.arsdigita.com/books/panda/


PHP: Hypertext Processing manual http://www.php.net/manual/


Doc 16, FastCGI and PHP Slide # 2

FastCGI

Background

Languages supported:

C, C++, Perl, Java, Python, TCL

Web servers supported:

Apache, Zeus

Netscape, Microsoft were supported
Third party vendor was bought out, currently not supplying the required modules
Basic Idea

FastCGI program runs all the time

Web server communicates with FastCGI program via socket

Socket connection stays open



Web server will restart FastCGI program if needed

FastCGI programs can be run as CGI program

Wrapper program available so CGI program can run as FastCGI


Doc 16, FastCGI and PHP Slide # 3
Fast CGI

Pros:


Cons




Doc 16, FastCGI and PHP Slide # 4

Fast CGI & Java


FastCGI classes map Standard.out to server socket

FCGIInterface reads data from server stream and places CGI environment variables in System properties (dictionary)


Standard FastCGI loop

   FCGIInterface serverConnection = new FCGIInterface().
   while(serverConnection.FCGIaccept()>= 0) {
      handle the new request here
   }


FCGIaccept()

Accepts connection from server if not open
Waits for a server request
Processes data in request (form data in System properties)


Doc 16, FastCGI and PHP Slide # 5

Count Example

import com.fastcgi.*;
public class FastCGICount 
   {
   public static void main(String args[]) 
      { 
      int count = 0;
      FCGIInterface serverConnection = new FCGIInterface().
      while(serverConnection.FCGIaccept()>= 0) 
         {
         count ++;
         System.out.println("Content-type: text/html\n\n");
         System.out.println("<html>");
         System.out.println("<head><TITLE>Hello</TITLE></head>");
         System.out.println("<body>");
         System.out.println("<H3>Hello</H3>");
         System.out.println("request number " + count );
         System.out.println("</body>");
         System.out.println("</html>"); 
         }   
      }
   }

Doc 16, FastCGI and PHP Slide # 6

Printing Environment Variables


import com.fastcgi.*;
import java.io.IOException;

public class FastCGICount  {
   public static void main(String args[])  {
      FCGIInterface intf = new FCGIInterface();
      while(intf.FCGIaccept()>= 0)  {
         System.out.println("Content-type: text/html\n\n");
         System.out.println("<html>");
         System.out.println("<head><TITLE>echo</TITLE></head>");
         System.out.println("<body>");         
         System.out.println("<H2>FastCGI echo</H2>");
         System.out.println("<H3>Environment Variables:</H3>");
         Properties environment = System.getProperties();
         Enumeration names = environment.propertyNames();
         while (names.hasMoreElements() ) {
            String name = (String) names.nextElement();
            String value = formData.getProperty(name);
            System.out.println("Name: " + name + " Value: " + value);
         }
         System.out.println("</body>");
         System.out.println("</html>");
         }
      }

Doc 16, FastCGI and PHP Slide # 7

Active Web Pages

Basic Idea

Imbed code in html pages

When the web server reads a page the imbedded code is run

Examples

Java Server Pages
ASP
PHP
Etc.

Pros

Scalable
Put web server on one machine
Web pages access data from database on another machine
Keep parts of program together

Scripting languages designed for text manipulation


Cons
Testing

Some scripting languages are owned by one company

Doc 16, FastCGI and PHP Slide # 8

PHP Examples

File: hello.php3


<HTML>
<BODY>
    This is a php example<BR>
    <?php 
        //Support C++ sytle comments
        echo "<B>Hello World</B><BR>";
        echo "How are you?<BR>"
    ?>
    <?  /*A short version of the tag
          which is not always enabled */
        $a = 1;         // $ indicates a variable
        $b = 2;
        echo "$a plus $b is  $a + $b <BR>";
        // Single quotes don't evaluate variables
        echo '$a plus $b is  $a + $b <BR>';
    ?>
    <script language="php">
        echo "Some html editors do not like <?php tags"
    </script>
</BODY>
</HTML>

http://www.eli.sdsu.edu/courses/fall00/cs580/notes/fastcgi/phpExamples/hello.php3


Doc 16, FastCGI and PHP Slide # 9
PHP Settings

PHP has a lot of useful build in functions

The phpinfo() method prints out the setting for php

File: settings.php3

<HTML>
<BODY>
   <?php phpinfo() ?>
</BODY>
</HTML>

http://www.eli.sdsu.edu/courses/fall00/cs580/notes/fastcgi/phpExamples/settings.php3


Doc 16, FastCGI and PHP Slide # 10

Form Data


Following example is used to demo php form variables

File formTest.html
<HTML>
<BODY>
   <CENTER>Get version</CENTER>
   <FORM    
   ACTION="formTest.php3" 
   METHOD="GET">
   ID <INPUT TYPE="text" NAME="id" SIZE= 30><P>
   Password <INPUT TYPE="text" NAME="password" SIZE= 30><P>
   Course <INPUT TYPE="text" NAME="course" SIZE= 30><P>
   <INPUT TYPE="submit" VALUE="Submit"> 
</FORM>
   <CENTER>Post version</CENTER>
<FORM    
   ACTION="formTest.php3" 
   METHOD="POST">
   ID <INPUT TYPE="text" NAME="id" SIZE= 30><P>
   Password <INPUT TYPE="text" NAME="password" SIZE= 30><P>
   Course <INPUT TYPE="text" NAME="course" SIZE= 30><P>
   <INPUT TYPE="submit" VALUE="Submit"> 
</FORM>
</BODY>
</HTML>
The above is the contents of:
http://www.eli.sdsu.edu/courses/fall00/cs580/notes/fastcgi/phpExamples/formTest.html


Doc 16, FastCGI and PHP Slide # 11
Form Data in PHP

Forms contain name/value pairs

Names become variables in the PHP page a form is sent to

The variable contains the value of the form name/value pair

File: formTest.php3
<HTML>
<BODY>
    <?php 
        echo "ID = $id<BR>";
        echo "Password = $password<BR>";
        echo "Course = $course<BR>";
    ?>
</BODY>
</HTML>
http://www.eli.sdsu.edu/courses/fall00/cs580/notes/fastcgi/phpExamples/formTest.html


Doc 16, FastCGI and PHP Slide # 12

Functions

This example shows how to define a function

File: GradeReporter1.php3
<HTML>
<BODY>
   <?php
      function login ($id, $course) {
         return "<FORM    
            ACTION=\"gradeReporter1.php3\" 
            METHOD=\"GET\">
            ID <INPUT TYPE=\"text\" SIZE= 30 NAME=\"id\" VALUE=$id><P>
            Password <INPUT TYPE=\"password\" NAME=\"password\" 
               SIZE= 30><P>
            Course <INPUT TYPE=\"text\" SIZE= 30NAME=\"course\" 
               VALUE=$course ><P>
            <INPUT TYPE=\"submit\" VALUE=\"Submit\"> 
            </FORM>";
      }
   ?>
   <?php
      echo login( "", "" )
   ?>
</FORM>
</BODY>
</HTML>
http://www.eli.sdsu.edu/courses/fall00/cs580/notes/fastcgi/phpExamples/gradeReporter2.php3


Doc 16, FastCGI and PHP Slide # 13

IF Statements


File gradeReporter2.php3
<HTML>
<BODY>
    <?php
        function login ($id, $course) { //Similar to last slide
        }
    ?>
    <?php
        $idLength = strlen($id);
        $courseLength = strlen($course);
        $passwordLength = strlen($password);
        //first login attempt
        if (($idLength == 0) && ($courseLength == 0) &&
            ($passwordLength == 0)) {
            echo login( " ", "" ) . "\n</BODY>\n</HTML>";
            die();
        };
        //Missing fields
        if (($idLength == 0) || ($courseLength == 0) ||
            ($passwordLength == 0)) {
            echo "You must provide all fields";
            echo login( $id , $course ) . "\n</BODY>\n</HTML>";
            die();
        };
    ?>
</FORM>
</BODY>
</HTML>
http://www.eli.sdsu.edu/courses/fall00/cs580/notes/fastcgi/phpExamples/gradeReporter2.php3


Doc 16, FastCGI and PHP Slide # 14
About gradeReporter2.php3

PHP borrows from C and PERL

Control structures are like C

if
if else
if elseif
while
do..while
for
foreach
switch


Concatenation of Strings

The period "." is used to concatenate strings


die()

This function ends the processing of the file

I don't like using this, but I am new to PHP and can't find a return or exit

Doc 16, FastCGI and PHP Slide # 15

Arrays

File array.php3
<HTML>
<BODY>
    Array Example<BR>
    <? 
        $a[0] = "cat";
        $a["mouse"] = "cheese";
        $a[2] = "jump";
        $a["dog"] = 5;
        list( $value ) = current($a);
        echo "First value: $value <BR>";
        list( $value ) = next($a);
        echo "Next value $value <BR>";
        list( $value ) = next($a);
        echo "Next value $value <BR>";
        list( $value ) = next($a);
        echo "Next value $value <BR>";
    ?>
</BODY>
</HTML>

http://www.eli.sdsu.edu/courses/fall00/cs580/notes/fastcgi/phpExamples/array.php3


Doc 16, FastCGI and PHP Slide # 16
About Arrays

An array is both a hash table (associative array) and an indexed array

$a[0] = "cat";
$a["dog"] = 5;



Doc 16, FastCGI and PHP Slide # 17

Reading Files

<HTML>
<BODY>
    <?php
        function login ($id, $course) { \\same as before
        }
    ?>
    <?php
     \\ login checks as before
    ?>
    <?php
        $fcontents = file ('passwordFile');
        while (list ($line_num, $line) = each ($fcontents)) {
            list($realID, $realPassword) = explode(":",Chop( $line));
            $passwords[$realID] = $realPassword;
        }
        if ($passwords[$id] != $password) {
            echo "Invalid password, try again";
            echo login( $id, $course );
            die();
        }
        if (! file_exists(str_replace(" ","",$course) )) {
            echo "Course $course does not exist";
            echo login( $id, $course ). "\n</BODY>\n</HTML>";
            die();
        }
    ?>
You made it
</FORM></BODY></HTML>
http://www.eli.sdsu.edu/courses/fall00/cs580/notes/fastcgi/phpExamples/gradeReporter3.php3

Doc 16, FastCGI and PHP Slide # 18
About GradeReporter3

$fcontents = file ('passwordFile');

File returns as array.
Each array element is on line in the file

while (list ($line_num, $line) = each ($fcontents)) {
each() iterates through the elements of the array.
Returns the index and value as an array

list ($line_num, $line)

Assigns the two elements of the array to $line_num and $line

list($realID, $realPassword) = explode(":",Chop( $line));
Chop removes whitespace from end of a string

Explode divides the string into array of strings separated by ":"

$passwords[$realID] = $realPassword;
Sets the password array

Doc 16, FastCGI and PHP Slide # 19

if (! file_exists(str_replace(" ","",$course) )) {


str_replace(" ","",$course) removes all spaces from the string

file_exists return TRUE (non zero) if the file exists


Doc 16, FastCGI and PHP Slide # 20

The Complete Example

<HTML><BODY>
   <?php
      function login ($id, $course) {
         return "<FORM   
            ACTION=\"gradeReporter3.php3\" 
            METHOD=\"GET\">
            ID <INPUT TYPE=\"text\" SIZE=30 NAME=\"id\" VALUE=$id ><P>
            Password <INPUT TYPE=\"password\" NAME=\"password\" SIZE= 30><P>
            Course <INPUT TYPE=\"text\" SIZE=30 NAME=\"course\" VALUE=$course ><P>
            <INPUT TYPE=\"submit\" VALUE=\"Submit\"> 
            </FORM>";
      }
   ?>
   <?php
      $idLength = strlen($id);
      $courseLength = strlen($course);
      $passwordLength = strlen($password);
      if (($idLength == 0) && ($courseLength == 0) &&
         ($passwordLength == 0)) {
         echo login( " ", "" ). "\n</BODY>\n</HTML>";
         die();
      };
      if (($idLength == 0) || ($courseLength == 0) ||
         ($passwordLength == 0)) {
         echo "You must provide all fields";
         echo login( $id , $course ). "\n</BODY>\n</HTML>";
         die();
      };
      $fcontents = file ('passwordFile');
      while (list ($line_num, $line) = each ($fcontents)) {
         list($realID, $realPassword) = explode(":",Chop( $line));
         $passwords[$realID] = $realPassword;
      }
      if ($passwords[$id] != $password) {
         echo "Invalid password, try again";
         echo login( $id, $course ). "\n</BODY>\n</HTML>";
         die();
      }
      if (! file_exists(str_replace(" ","",$course) )) {
         echo "Course $course does not exist";
         echo login( $id, $course ). "\n</BODY>\n</HTML>";
         die();
      }
   ?>You made it</FORM></BODY></HTML>

Copyright ©, All rights reserved.
2000 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 25-Oct-00    Next