Things we’ll 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 • Three-tier architecture • CGI basics Client Program (Web Browser) Presentation tier • PHP overview • PHP classes and functions Middle tier • PHP files Application Server • PHP arrays Data management Database Management tier System, File Server 2
Example 1: Airline reservations Technologies Build a system for making airline reservations Client Program (Web Browser) • Database/File System HTML, Javascript, XSLT • Application Server C++, Cookies, XML, Application Server XPath, web services, Perl, PHP • Client Program Database / File System SQL, Triggers, Stored Procedures, Scripts 3
PHP PHP Strengths • High performance • PHP: PHP Hypertext Preprocessor • Interface to many different database systems • Server-side scripting language • Built-in libraries • Ease of learning and use • PHP pages require a web server with PHP support • Object-oriented support • Portability • Competitors: • Open source • Free • Availability of support 4
PHP References CGI – What does it all look like? • Online tutorials – http://www.w3schools.com/php/default.asp – https://www.codecademy.com/learn/php • Online references – http://www.php.net • IT350 Textbook: Internet & WWW – How to Program by Deitel, Deitel, and Goldberg. 5
CGI Script Basics How can CGI get data from user? Technique #1: Forms • Common Gateway Interface (CGI) • User enters data via a form, submits – “Common”: Not specific to any operating system or language • Browser requests the CGI file by name specified on form __________ • Output file generated at runtime: • Web server runs the CGI program with form’s data 1. When a program executed as a CGI script, “standard output” passed to it is redirected to web server 2. Web server then redirects output to client's browser • Script receives data in one of two ways: 1. method = “get” 2. method = “post” Use language-specific method to get these inside CGI program Technique #2: URL with parameters <a href= “ http://www.usna.edu/CS/calendar/view.pl?events=seminars ”> Seminars </a> 6
PHP Overview PHP and CGI • PHP tags <?php ?> • We will use PHP in this class. – Mixed with HTML tags – File extension .php – We will not focus on teaching you the language. • Statements – You are expected to pick this up on your own. – Separated by semicolon – if..else.., while, do, for, switch – echo • Remember: the goal of a CGI program is to • Variables output HTML – $varname – Type determined by content; type not declared, variables not declared – The web server will run your program. – Variable names are case sensitive – The web server will pipe the output to your browser. • Strings – Your browser only understands HTML – Single quotes – literal string – Double quotes – interpolated string – Use . to concatenate • Accessing form variables – $_POST[‘age’], $_GET[‘age’], $_REQUEST[‘age’] 7
PHP Objects and Functions Exercise #1 • PHP objects • Write PHP code that will, given the URL provided below, – Java-like inheritance generate HTML that looks like the screenshot – public, private, or protected attributes and methods http://mope.academy.usna.edu/~adina/it350/ice/set12_ex1.php?maxNumber=5 – __construct(), __destruct(), – __set(), __get() • PHP functions – function myFunction ($param1, $param2){…return .. } 8
How everything works: page.inc.php <?php class Page{ Step 1: Input Form – form.html public $content; private $title; <!DOCTYPE html> public function __construct($title){ $this->title = $title; <html><head><title>IT350 PHP test page</title><meta charset = “utf - 8”></head> } <body> <form action="processPersonInfo.php" method="post"> public function __set($name, $value){ $this->$name = $value; } <p><label>Enter your name: <input type="text" name="name"/></label></p> public function display(){ ?> <p><label>Enter your age: <input type="text" name="age" /></label></p> <!DOCTYPE html> <head><title> <?php echo $this->title; ?> </title></head> <body> <p><input type="submit" name = "submit" value = "Submit"/></p> <?php echo $this->content; echo "</body></html>"; } </form> } //end class definition </body> </html> ?> 9
Step 1 version 2 – getPersonInfo.php Exercise #2 • Create Person class with method <?php getPersonAttributesAsHTMLInput() that returns '<p><label>Enter your name: <input type="text” require('page.inc.php'); name="name“/></label></p>’. ‘<p><label>Enter your age: <input type="text" name="age"/></label></p>’ $page = new Page("Input person"); • Modify getPersonInfo.php to use the getPersonAttributesAsHTMLInput $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>’. method in Person class '<input type = "submit" value = "submit">'; $page->display(); ?> 10
person.inc.php – part 2 person.inc.php – part 1 <?php /* define a class Person with name and age */ public function processPerson(){ class Person{ $success = $this->insertToFile(); private $name; private $age; if ($success){ $confirmation = '<h1>Thank you for registering with our site</h1>'. public function __construct(){} '<p>The information recorded is as follows: <br />'. "Name: $this->name <br /> Age: $this->age </p>"; public function __set($varName, $varValue) } { else{ $varValue = trim($varValue); $confirmation = '<h1>Error: we had problems with your $varValue = strip_tags($varValue); registration (probably some file error - permissions??). $this->$varName = $varValue; Please try again.</h1>'; } } return $confirmation; public function __get($varName) } { return $this->$varName; } 11
person.inc.php – part 3 PHP Files private function insertToFile($fileName="persons.txt") { • Files $fp = @fopen($fileName, 'a'); – $handle = fopen($fileName, $mode) if (!$fp){ return false; – fwrite($handle, $someText) } – fclose($handle) else{ – fgets($handle) $text = "$this->name\t$this->age\n"; fwrite($fp, $text); – feof ( $handle ) fclose($fp); return true; } } 12
person.inc.php – part 4 Step 2: processPersonInfo.php public static function getAllPersonsInfo($fileName = "persons.txt"){ <?php $fp = @fopen($fileName, 'r'); require('page.inc.php'); require('person.inc.php'); if (!$fp){ $content = "<p>ERROR! Could not open file $fileName for $name = $_POST['name']; $age = $_POST['age']; reading.</p>"; } $page = new Page("Registration confirmation"); else{ $content= '<p>Here is the list: <br>'; if (empty($name) || empty($age)){ $line = fgets($fp); $page->content = '<p> Name or age not entered!! Try again</p>'; while( !feof($fp) ){ $page->display(); exit; $content .= $line . '<br>'; } $line = fgets($fp); $dummy = new Person(); } $dummy->name = $name; $content .= '</p>'; $dummy->age = $age; fclose($fp); } $page->content = $dummy->processPerson(); return $content; $page->display(); ?> } }?> 13
< ?php Step 3: readPearsonsInfo.php CGI – Review 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(); ?> 14
Recommend
More recommend