1
IT350: Web & Internet Programming
Set 12: CGI and PHP
Things we’ll learn and do
- HTML5 – basics, tables, forms
- Cascading Style Sheets
- JavaScript
- Dynamic HTML
- CGI / PHP
IT350: Web & Internet Programming JavaScript Dynamic HTML CGI - - PowerPoint PPT Presentation
Things well learn and do HTML5 basics, tables, forms Cascading Style Sheets IT350: Web & Internet Programming JavaScript Dynamic HTML CGI / PHP Set 12: CGI and PHP 1 Outline The Three-Tier Architecture
Presentation tier Client Program (Web Browser) Middle tier Application Server Database Management System, File Server Data management tier
Client Program (Web Browser) Application Server Database / File System HTML, Javascript, XSLT C++, Cookies, XML, XPath, web services, Perl, PHP SQL, Triggers, Stored Procedures, Scripts
1. method = “get” 2. method = “post” Use language-specific method to get these inside CGI program
<a href=“http://www.usna.edu/CS/calendar/view.pl?events=seminars”> Seminars </a>
?>
– Mixed with HTML tags – File extension .php
– Separated by semicolon – if..else.., while, do, for, switch – echo
– $varname – Type determined by content; type not declared, variables not declared – Variable names are case sensitive
– Single quotes – literal string – Double quotes – interpolated string – Use . to concatenate
– $_POST[‘age’], $_GET[‘age’], $_REQUEST[‘age’]
http://mope.academy.usna.edu/~adina/it350/ice/set12_ex1.php?maxNumber=5
<!DOCTYPE html> <html><head><title>IT350 PHP test page</title><meta charset = “utf-8”></head> <body>
<form action="processPersonInfo.php" method="post"> <p><label>Enter your name: <input type="text" name="name"/></label></p> <p><label>Enter your age: <input type="text" name="age" /></label></p> <p><input type="submit" name = "submit" value = "Submit"/></p> </form>
</body> </html>
<?php class Page{ public $content; private $title; public function __construct($title){ $this->title = $title; } public function __set($name, $value){ $this->$name = $value; } public function display(){ ?> <!DOCTYPE html> <head><title> <?php echo $this->title; ?> </title></head> <body> <?php echo $this->content; echo "</body></html>"; } } //end class definition ?>
page.inc.php
<?php require('page.inc.php'); $page = new Page("Input person"); $page->content = '<form action = "processPersonInfo.php" method = "post">'. '<p><label>Enter your name: <input type="text” name="name“/></label></p>’. ‘<p><label>Enter your age: <input type="text" name="age"/></label></p>’. '<input type = "submit" value = "submit">'; $page->display(); ?>
'<p><label>Enter your name: <input type="text” name="name“/></label></p>’. ‘<p><label>Enter your age: <input type="text" name="age"/></label></p>’
<?php /* define a class Person with name and age */ class Person{ private $name; private $age; public function __construct(){} public function __set($varName, $varValue) { $varValue = trim($varValue); $varValue = strip_tags($varValue); $this->$varName = $varValue; } public function __get($varName) { return $this->$varName; }
person.inc.php – part 1
public function processPerson(){ $success = $this->insertToFile(); if ($success){ $confirmation = '<h1>Thank you for registering with our site</h1>'. '<p>The information recorded is as follows: <br />'. "Name: $this->name <br /> Age: $this->age </p>"; } else{ $confirmation = '<h1>Error: we had problems with your registration (probably some file error - permissions??). Please try again.</h1>'; } return $confirmation; }
person.inc.php – part 2
private function insertToFile($fileName="persons.txt") { $fp = @fopen($fileName, 'a'); if (!$fp){ return false; } else{ $text = "$this->name\t$this->age\n"; fwrite($fp, $text); fclose($fp); return true; } }
person.inc.php – part 3
public static function getAllPersonsInfo($fileName = "persons.txt"){ $fp = @fopen($fileName, 'r'); if (!$fp){ $content = "<p>ERROR! Could not open file $fileName for reading.</p>"; } else{ $content= '<p>Here is the list: <br>'; $line = fgets($fp); while( !feof($fp) ){ $content .= $line . '<br>'; $line = fgets($fp); } $content .= '</p>'; fclose($fp); } return $content; } }?>
person.inc.php – part 4
<?php require('page.inc.php'); require('person.inc.php'); $name = $_POST['name']; $age = $_POST['age']; $page = new Page("Registration confirmation"); if (empty($name) || empty($age)){ $page->content = '<p> Name or age not entered!! Try again</p>'; $page->display(); exit; } $dummy = new Person(); $dummy->name = $name; $dummy->age = $age; $page->content = $dummy->processPerson(); $page->display(); ?>
Step 2: processPersonInfo.php
<?php
require('page.inc.php'); require('person.inc.php'); if (isset($_GET['filename'])){ $fileName = $_GET['filename']; } else{ $fileName = "persons.txt"; } $page = new Page("Persons list"); $page->content = Person::getAllPersonsInfo($fileName); $page->display(); ?> Step 3: readPearsonsInfo.php
<?php require_once('page.inc.php'); require_once('arrayFunctions.inc.php'); $myPage = new Page('Array examples'); $myPage->content = '<p>Working with Arrays</p>'; $array = array('thing1', 'thing2'); $array[2] = 'thing3'; $myPage->content .= '<p>'.displayArray($array) . '</p>'; $prices = array('Tires' => 100, 'Oil' => 10); $prices[‘Spark plugs'] = 200; $myPage->content .= '<p>The values in $prices array are <br />’ . displayAssociativeArray($prices) . '</p>'; sort($array); //sort array $myPage->content .= '<p> Sorted array '.displayArray($array) . '</p>'; ksort($prices); //sort associative array by key $myPage->content .= '<p>The values in $prices array sorted by key are <br />' . displayAssociativeArray($prices) . '</p>'; asort($prices); //sort associative array by value $myPage->content .= '<p>The values in $prices array sorted by value are <br />' . displayAssociativeArray($prices) . '</p>'; $myPage->display(); ?>
<?php //return a string to display an array function displayArray($array){ $result = ‘<p>'; for($i = 0; $i < count($array); $i++){ $result .= "Element at index $i is $array[$i] <br />"; } $result .= ‘</p>'; return $result; } //return a string to display an associative array function displayAssociativeArray($array){ $result = ‘<p>'; foreach($array as $key => $value){ $result .= "Element with key $key has value $value <br />"; } $result .= ‘</p>'; return $result; } ?>
<?php function fn(){ $x = 'content'; } fn(); echo 'Value of $x is '. $x; ?>
<?php $x = 'content 1 <br/>'; echo 'Content of $x after initialization is '. $x . '<br />'; function fn(){ echo 'Content of $x at start of function is '. $x. '<br />'; $x = 'content 2 <br/>'; echo 'Content of $x at end of function is '. $x. '<br />'; } fn(); echo 'Value of $x after calling fn() is '. $x. ‘<br />'; ?>
?>
– Mixed with HTML tags – File extension .php
– Separated by semicolon – if..else.., while, do, for, switch, echo
– $varname – Type determined by content; variables not declared; case sensitive
– Single quotes – literal string – Double quotes – interpolated string (variables are replaced with their value)
– Numerically indexed arrays – Associative arrays – count(), foreach($array as $key => $value)…
– $_POST[‘age’], $_GET[‘age’], $_REQUEST[‘age’]