CS 580 Client-Server Programming Fall Semester, 2000 FastCGI and PHP |
||
---|---|---|
© 2000, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 25-Oct-00 |
FastCGI
Background
Languages supported:
Fast CGI
Pros:
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()
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>"); } } }
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>"); } }
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
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
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
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
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
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
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
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 |
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
About Arrays
An array is both a hash table (associative array) and an indexed array
$a[0] = "cat";
$a["dog"] = 5;
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
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
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
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