Download presentation
Presentation is loading. Please wait.
1
1 CS 482 Web Programming and Scripting Week 6 – Installing Apache, PHP, MySQL and Using the Software
2
2 Topics Apache Web Server PHP MySQL
3
3 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)
4
4 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
5
5 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
6
6 Three-Tier Application Model Database Application Client Tier Information Tier Middle Tier
7
7 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
8
8 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
9
9 Apache Web Server Currently the most popular Web server Stability Efficiency Portability Open-source
10
10 Apache Web Server Don’t count on it!
11
11 Requesting Documents Requesting two different documents XHTML PHP
12
12 XHTML Request XHTML documents from Apache Launch Browser Enter XHTML document’s location in Address field USE FACILITATOR GUIDE FOR INSTALLATION INSTRUCTIONS AND TUTORIAL!
13
13 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
14
14 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
15
ClientServer Joe Web Browser Windows OS http://my.server.com/webmail.php HTTP request Apache PHP MySQL HTTP response Linux OS SQL query Result set LAMP Development Framework
16
16 LAMP 1. 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 2. 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
17
17 LAMP 3. 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 4. The results returned by the interpreter are transmitted to Joe’s browser by the Apache server
18
18 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
19
19 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
20
20 PHP Basic application Scripting delimiters Must enclose all script code Variables preceded by $ symbol Case-sensitive End statements with semicolon Comments // for single line /* */ for multiline Filenames end with.php by convention
21
21 Declare variable $name Scripting delimiters Single-line comment Function print outputs the value of variable $name PHP First Program Example
22
22 PHP Output
23
23 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
24
24 PHP Data Types
25
25 Assign a string to variable $testString Assign a double to variable $testDouble Assign an integer to variable $testInteger PHP Data Types Example
26
26 Print each variable’s value 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
27
27 Use type casting to cast variable $data to different types
29
29 PHP Operators Arithmetic operators Assignment operators Syntactical shortcuts Before being assigned values, variables have value undef Constants Named values define function
30
30 Define constant VALUE.Add constant VALUE to variable $a. PHP Operators Example
31
31 Multiply variable $a by two using the multiplication assignment operator *=. Test whether variable $a is less than 50 Add 40 to variable $a using the addition assignment operator +=. Print if variable $a is less than 50.
32
32 Add constant VALUE to an uninitialized variable. Add a string to an integer. Print an uninitialized variable ( $nothing ).
34
34 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
35
35 More PHP... Arrays, cont. Built-in iterators Maintain pointer to element currently referenced reset key next foreach loops
36
36 PHP Keywords
37
37 Create the array $first by assigning a value to an array element. Assign a value to the array, omitting the index. Appends a new element to the end of the array. Use a for loop to print out each element’s index and value. Function count returns the total number of elements in the array. PHP Array Example
38
38 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 reset sets the internal pointer to the first element of the array. Function key returns the index of the element which the internal pointer references. Function next moves the internal pointer to the next element.
39
39 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.
41
41 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
42
42 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
43
43 Use relational operators to compare each array element to string “apple”.
45
45 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 [ ]
46
46 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
47
47 Viewing Client/Server Environment Variables
48
48 Form Processing and Business Logic Form processing action property Where to send form data method property post Each element has unique name
49
49 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
50
50 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.
51
51
52
52
54
54 Form Processing and Business Logic Business logic Confirm that valid information was entered extract function Creates variables corresponding to each key-value pair in array Easily retrieve all values sent to PHP page Regular expressions very helpful Do checks on client side where possible JavaScript Conserves server resources Ending a script die function Remember to close all HTML tags
55
55 Function ereg is called to determine whether the phone number entered by the user is valid. The expression \( matches the opening parentheses of a phone number. We access the phone field’s value from form.html by using variable $phone. The parentheses in the expression must be followed by three digits ( [0-9]{3} ), a closing parenthesis, three digits, a literal hyphen and four additional digits. Read information sent from form
56
56 Function die terminates script execution
57
57
58
58
59
59 Your Text has some great examples! Chapter 26 – PHP and MySQL User ID and Password validation using a text file Form processing Querying a database Dynamic displays of database data Cookies and manipulation of cookies
60
60 Connecting to a Database Databases Store and maintain data MySQL is a free database product PHP supports many database operations Access databases from Web pages
61
61 Connecting to a Database Interacting with databases SQL Structured Query Language Used to manipulate databases Several useful functions mysql_connect mysql_select_db mysql_query mysql_error mysql_fetch_row mysql_close
62
62 PHP Resources www.php.net www.phpbuilder.com www.phpworld.com www.phpwizard.net
63
63 SQL and MySQL Resources www.sqlcourse.com www.w3schools.com/sql/sql_quickref.as p www.w3schools.com/sql/sql_quickref.as p www.mysql.com
64
64 Consolidated - Nice site! http://www.everythingphpmysql.com/li nks/index.html http://www.everythingphpmysql.com/li nks/index.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.