Course Topics � Database design IT420: Database Management and � Relational model Organization � SQL � Normalization � PHP � MySQL Introduction to PHP � Database administration � Transaction Processing � Data Storage and Indexing The Three-Tier Architecture Example 1: Airline reservations Build a system for making airline reservations Client Program (Web Browser) Presentation tier � Database System Middle tier Application Server � Application Server � Client Program Data management Database Management tier System Three-Tier Architecture: Technologies Advantages � Heterogeneous systems Client Program (Web Browser) HTML, Javascript, XSLT � Thin clients C++, Cookies, XML, Application Server XPath, web services, � Integrated data access Perl, PHP � Scalability SQL, Triggers, Database Management Stored Procedures System � Software development 1
Web Applications PHP � Need to choose: � PHP: PHP Hypertext Preprocessor � Operating system – Windows or Linux � Server-side scripting language � Web server software – Apache � Browser never sees PHP - only the web � Database Management System – MySQL server sees it � Programming or scripting language – PHP � PHP pages require a web server with PHP support � Competitors: Perl, ASP.NET, JSP PHP Strengths PHP References � High performance � Online references � Interface to many different database systems � http://www.php.net � Built-in libraries � Online Tutorial � Ease of learning and use � http://www.w3schools.com/php/default.asp � Object-oriented support � PHP and MySQL Web Development by � Portability Luke Welling and Laura Thomson � Open source � Free � IT350 textbook: Internet & WWW How To � Availability of support Program by Deitel, Deitel, and Goldberg CGI – What does it all look like? IT350 - CGI Script Basics � Common Gateway Interface (CGI) � “Common”: Not specific to any operating system or language � Output file generated at runtime: 1. When a program executed as a CGI script, “standard output” is redirected to client Web server 2. Web server then redirects output to client's browser 2
IT350 - How can CGI get data Form Processing from user? Web Server Browser User enters • Server receives form. Technique #1: Forms information into • The form’s action is the � User enters data via a form, submits the form then name of the PHP script � Form directs results to a CGI program “Submits” the • PHP engine parser form � Script receives data in one of two ways: decodes the form contents and variables then 1. Method = “GET” processes the script 2. Method = “POST” Use language-specific method to get these inside CGI program Results sent back to the User sees the Technique #2: URL with parameters browser. Connection closes. results. <a href=http://www.cs.usna.edu/calendar/view.php?events=seminars> Seminars </a> Input Form – form.html <?xml version = "1.0" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html><head><title>IT420 PHP test page</title></head> <body> <h2>Survey</h2> <form action="process_name_age_oo.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 process_name_age_oo.php process_name_age_oo.php /* get the input parameters. The input parameters can be either from a form, or from the address bar */ $name = $_POST['name']; <?php $age = $_POST['age']; require('class_page.inc.php'); //define constants function save_to_file($text, $fileName = "myFile.txt"){ define('YEAR99',99); $fp = @fopen($fileName, 'a'); if (!$fp){ //create a page object echo "<p>ERROR: Could not open file $fileName. </p>"; $page = new Page(); return FALSE; $page->title = "IT420 test PHP"; } //check input parameters else{ if (empty($name) || empty($age)){ fwrite($fp, $text); $page->content = '<p> Name or age not entered!!</p>'; fclose($fp); } return TRUE; else{ } //output some response to the user $page->content = "<p>Welcome $name!<br /> You are $age years old! <br />"; } $page->content=$page->content."Your age plus ".(YEAR99-$age)." is ".YEAR99. '</p>'; ?> //save information to file save_to_file("$name\t$age\n"); } //display the page to the browser $page->Display(); ?> 3
read_name_age_files.php class_page.inc.php <?php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" class Page{ "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> //attributes <html><head><title>IT420 test PHP files</title></head> public $content; <body> public $title = "IT420 page"; <?php private $header = "<?xml version = '1.0' ?> $fileName = $_GET['filename']; <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>"; @ $fp = fopen($fileName, 'r'); //constructor if (!$fp){ public function __construct(){ } echo "<p>ERROR! Could not open file $fileName for reading.</p>"; } //set public attributes public function __set($name, $value){ else{ $this->$name = $value; echo '<p>'; } $line = fgets($fp); //display page public function display(){ while( !feof($fp) ){ echo $this->header; echo $line.'<br />'; echo "<head><title> $this->title </title></head>"; $line = fgets($fp); echo "<body>"; echo $this->content; } echo "</body></html>"; } fclose($fp); } //end class definition ?> echo '</p>'; } ?> </body></html> Class Exercise � Write PHP code that will, given the URL provided below, generate HTML that looks like the screenshot http://www.adina.it420.cs.usna.edu/ice1.php?maxNumber=5 PHP Summary PHP Summary � PHP objects � PHP tags <?php ?> � Mixed with HTML tags � Java-like inheritance � File extension .php � public, private, or protected attributes and methods � Statements � __construct(), __destruct(), � Separated by semicolon � if..else.., while, do, for, switch � __set(), __get() � Variables � PHP functions � $varname � Type determined by content; variables not declared; case sensitive � function myFunction($param1, $param2){…} � Strings � Files � Single quotes – literal string � resource fopen( string $fileName, string $mode) � Double quotes – interpolated string (variables are replaced with their value) � int fwrite( resource $handle, string $someText) � Accessing form variables � int fclose( resource $handle) � $_POST[‘age’], $_GET[‘age’] (if method is GET), $_REQUEST[‘age’] � string fgets( resource $handle) � boolean feof (resource $handle ) 4
Recommend
More recommend