640 likes | 728 Views
CS 482 Web Programming and Scripting. Week 6 – Installing Apache, PHP, MySQL and Using the Software. Topics. Apache Web Server PHP MySQL. Introduction. Web server Responds to client requests by providing resources such as XHTML documents
E N D
CS 482 Web Programming and Scripting Week 6 – Installing Apache, PHP, MySQL and Using the Software
Topics • Apache Web Server • PHP • MySQL
Introduction • Web server • Responds to client requests by providing resources such as XHTML documents • Web server maps the URL (Uniform Resource Locator) to a resource on the server and returns the requested resource to the client • Web server and client communicate with platform-independent Hypertext Transfer Protocol (HTTP)
HTTP Request Types • Request methods • get – as part of URL • post – specified as XHTML form by the method “post” – sends form data as an HTTP message (not part of URL) • Retrieve and send client form data to Web server • Post data to a server-side form handler
System Architecture • Multi-tier application (n-tier application) • Information tier (data or bottom tier) • Maintains data for the application • Stores data in a relational database management system (RDBMS) • Middle tier • Implements business logic and presentation logic • Control interactions between application clients and application data • Client tier (top tier) • Application’s user interface • Users interact directly with the application through the client tier
Three-Tier Application Model Client Tier Application Middle Tier Database Information Tier
Client-Side Scripting versus Server-Side Scripting • Client-side scripts • Validate user input • Reduce requests needed to be passed to server • Access browser • Enhance Web pages with DHTML, ActiveX controls, and applets • Server-side scripts • Executed on server • Generate custom response for clients • Wide range of programmatic capabilities • Access to server-side software that extends server functionality
Accessing Web Servers • Request documents from Web servers • Host names • Local Web servers • Access through machine name or localhost • Remote Web servers • Access through machine name • Domain name or Internet Protocol (IP) address • Domain name server (DNS) • Computer that maintains a database of host names and their corresponding IP address
Apache Web Server • Currently the most popular Web server • Stability • Efficiency • Portability • Open-source
Apache Web Server Don’t count on it!
Requesting Documents • Requesting two different documents • XHTML • PHP
XHTML • Request XHTML documents from Apache • Launch Browser • Enter XHTML document’s location in Address field USE FACILITATOR GUIDE FOR INSTALLATION INSTRUCTIONS AND TUTORIAL!
Web Resources • www.w3.org/Protocols • www.apache.org • httpd.apache.org • httpd.apache.org/docs-2.0 • www.apacheweek.com • linuxtoday.com/stories/18780.html
LAMP • Linux, Apache, MySQL, and PHP • Linux provides the base operating system and server environment • Apache Web server intercepts HTTP requests and either serves them directly or passes them on to the PHP interpreter for execution • PHP interpreter parses and executes PHP code, and returns the results to the Web server • MySQL RDBMS serves as the data storage engine, accepting connections from the PHP layer and inserting, modifying, or retrieving data
LAMP Development Framework http://my.server.com/webmail.php HTTP request HTTP response Web Browser Apache MySQL Windows OS PHP SQL query Result set Linux OS Joe Client Server
LAMP • Joe pops open his Web browser at home and types in the URL for his online Webmail client. After looking up the domain, Joe’s browser (the client) sends an HTTP request to the corresponding server IP address • The Apache Web server handling HTTP requests for the domain receives the request and notes that the URL ends with a .php suffix. Because the server is programmed to automatically redirect all such requests to the PHP layer, it simply invokes the PHP interpreter and passes it the contents of the named file
LAMP • The PHP interpreter parses the file, executing the code in the special PHP tags. If the code includes database queries, the PHP interpreter opens a client connection to the MySQL RDBMS and executes them. Once the script interpreter has completed executing the script, it returns the result to the browser, cleans up after itself, and goes back into hibernation • The results returned by the interpreter are transmitted to Joe’s browser by the Apache server
PHP • Request PHP documents from Apache • Save PHP documents in the htdocs directory – use .php filenames • Launch Browser • Enter PHP document’s location in Address field
PHP • PHP: Hypertext Preprocessor • Originally called “Personal Home Page Tools” • Popular server-side scripting technology • Open-source • Anyone may view, modify and redistribute source code • Supported freely by community • Platform independent
PHP • Basic application • Scripting delimiters • <? php ?> • Must enclose all script code • Variables preceded by $ symbol • Case-sensitive • End statements with semicolon • Comments • // for single line • /* */ for multiline • Filenames end with .phpby convention
Scripting delimiters Declare variable $name Single-line comment Function print outputs the value of variable $name PHP First Program Example
PHP Variables • Variables • Can have different types at different times • Variable names inside strings replaced by their value • Type conversions • settype function • Type casting • Concatenation operator • .(period) • Combine strings
Assign a string to variable $testString Assign a double to variable $testDouble Assign an integer to variable $testInteger PHP Data Types Example
Call function settype to convert the data type of variable $testString to a double. Call function settype to convert the data type of variable $testString to an integer. Convert variable $testString back to a string Print each variable’s value
PHP Operators • Arithmetic operators • Assignment operators • Syntactical shortcuts • Before being assigned values, variables have value undef • Constants • Named values • define function
Define constant VALUE. Add constant VALUE to variable $a. PHP Operators Example
Multiply variable $a by two using the multiplication assignment operator *=. Test whether variable $a is less than 50 Print if variable $a is less than 50. Add 40 to variable $a using the addition assignment operator +=.
Print an uninitialized variable ($nothing). Add constant VALUE to an uninitialized variable. Add a string to an integer.
More PHP... • Keywords • Reserved for language features • if…elseif…else • Arrays • Group of related data • Elements • Name plus braces and index • Indices start at zero • count function • array function
More PHP... • Arrays, cont. • Built-in iterators • Maintain pointer to element currently referenced • reset • key • next • foreach loops
Create the array $first by assigning a value to an array element. Use a for loop to print out each element’s index and value. Function count returns the total number of elements in the array. Assign a value to the array, omitting the index. Appends a new element to the end of the array. PHP Array Example
Call function array to create an array that contains the arguments passed to it. Store the array in variable $second. Assign values to non-numerical indices in array $third. Function key returns the index of the element which the internal pointer references. Function reset sets the internal pointer to the first element of the array. Function next moves the internal pointer to the next element.
Operator => is used in function array to assign each element a string index. The value to the left of the operator is the array index, and the value to the right is the element’s value.
String Processing and Regular Expressions • String processing • Equality and comparison two important operations • strcmp function • Returns –1 if string 1 < string 2 • Returns 0 if string 1 = string 2 • Returns 1 if string 1 > string 2 • Relational operators
Use a for loop to iterate through each array element. Function strcmp compares two strings. If the first string alphabetically precedes the second, then –1 is returned. If the strings are equal, 0 is returned. If the first string alphabetically follows the second, then 1 is returned. PHP String Comparision Example
Use relational operators to compare each array element to string “apple”.
String Processing and Regular Expressions • Regular expressions • Pattern matching templates • ereg function • POSIX • preg_match function • Perl • ereg_replace function • Building regular expressions • Metacharacters • $, ., ^ • Brackets [ ]
Viewing Client/Server Environment Variables • Environment variables • Provide information about execution environment • Type of Web browser • Type of server • Details of HTTP connection • Stored as array in PHP • $_ENV
Form Processing and Business Logic • Form processing • action property • Where to send form data • method property • post • Each element has unique name
The action attribute of the form element indicates that when the user clicks Register, the form data will be posted to form.php. PHP Using a Form Example
A unique name (e.g., email) is assigned to each of the form’s input fields. When Register is clicked, each field’s name and value are sent to the Web server.