Web Technologies Course Outline Christian KHOURY
[email protected]
References & Links • • • •
http://www.php.net/ http://www.php.net/manual/en/ http://www.mysql.com/ http://httpd.apache.org/
• Interesting link for website construction by example – http://www.siteduzero.com/tutoriel-3-14668-un-sitedynamique-avec-php.html
2
What is PHP ? • • • •
PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) • PHP is an open source software (OSS) • PHP is free to download and use
3
What is a PHP file ? • PHP files may contain text, HTML tags and scripts • PHP files are returned to the browser as plain HTML • PHP files have a file extension of ".php", ".php3", or ".phtml"
4
What is MySQL ? • MySQL is a database server • MySQL is ideal for both small and large applications • MySQL supports standard SQL • MySQL compiles on a number of platforms • MySQL is free to download and use PHP combined with MySQL are cross-platform
5
Where to start ? • Install an Apache server on a Windows or Linux machine • Install PHP on a Windows or Linux machine • Install MySQL on a Windows or Linux machine
6
PHP Syntax
7
Basic Syntax • A PHP scripting block always starts with • can be placed anywhere in the document • On servers with shorthand support enabled you can start a scripting block with
8
Simple Example • Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another 9
Comments in PHP
10
PHP Variables • All variables in PHP start with a $ sign symbol – $var_name = value;
11
PHP Variables • PHP is a Loosely Typed Language • In PHP a variable does not need to be declared before being set. • In the example above, you see that you do not have to tell PHP which data type the variable is. • PHP automatically converts the variable to the correct data type, depending on how they are set. • In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it. • In PHP the variable is declared automatically when you use it. 12
Naming Rules • A variable name must start with a letter or an underscore "_" • A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ ) • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)
13
Predefined Variables • • • • • • • • • • • • • • •
Superglobals — Superglobals are built-in variables that are always available in all scopes $GLOBALS — References all variables available in global scope $_SERVER — Server and execution environment information $_GET — HTTP GET variables $_POST — HTTP POST variables $_FILES — HTTP File Upload variables $_REQUEST — HTTP Request variables $_SESSION — Session variables $_ENV — Environment variables $_COOKIE — HTTP Cookies $php_errormsg — The previous error message $HTTP_RAW_POST_DATA — Raw POST data $http_response_header — HTTP response headers $argc — The number of arguments passed to script $argv — Array of arguments passed to script
14
Variable Scope • Entire php file • Global keyword
15
Using $GLOBALS
16
Static Variables • C-like
17
Strings (1/3) • Concatenation Operator – (.) is used to put two string values together – To concatenate two variables together, use the dot (.) operator
• In double-quoted strings, variable names will be expanded
18
Strings (2/3) • strlen() • strpos() – used to search for a string or character within a string – If a match is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE
19
Strings (3/3) – Output is 6 – The reason that it is 6, and not 7, is that the first position in the string is 0, and not 1
20
PHP Operators (1/4) • Arithmetic operators
21
PHP Operators (2/4) • Assignment operators – – – – – – –
= += -= /= *= .= %=
22
PHP Operators (3/4) • Comparison operators
23
PHP Operators (4/4) • Logical operators
24
Conditional Statements (1/3) • if...else statement - use this statement if you want to execute a set of code when a condition is true and another if the condition is not true • elseif statement - is used with the if...else statement to execute a set of code if one of several conditions are true
25
Conditional Statements (2/3)
• If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces 26
Conditional Statements (3/3)
• The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!" 27
Switch Statement (1/2) • used to avoid long blocks of if..elseif..else code
28
Switch Statement (2/2) • A single expression (most often a variable) is evaluated once • The value of the expression is compared with the values for each case in the structure • If there is a match, the code associated with that case is executed • After a code is executed, break is used to stop the code from running into the next case • The default statement is used if none of the cases are true 29
PHP Arrays There are three different kind of arrays: • Numeric array - An array with a numeric ID key • Associative array - An array where each ID key is associated with a value • Multidimensional array - An array containing one or more arrays
30
Numeric Arrays (1/2) • stores each element with a numeric ID key Examples: $names = array("Peter","Quagmire","Joe"); $names[0] = "Peter"; $names[1] = "Quagmire"; $names[2] = "Joe"; 31
Numeric Arrays (2/2)
32
Associative Arrays $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
33
Multidimensional Array
echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?";
34
Iterations In PHP we have the following looping statements: • while - loops through a block of code if and as long as a specified condition is true • do...while - loops through a block of code once, and then repeats the loop as long as a special condition is true • for - loops through a block of code a specified number of times • foreach - loops through a block of code for each element in an array 35
While Statement
36
do … while
37
for
38
foreach
39
PHP Functions (1/4) • In PHP - there are more than 700 built-in functions available ! • Creating PHP functions: – All functions start with the word "function()" – Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number) – Add a "{" - The function code starts after the opening curly brace – Insert the function code – Add a "}" - The function is finished by a closing curly brace
40
PHP Functions (2/4)
41
PHP Functions (3/4)
42
PHP Functions (4/4)
43
Form Handling (1/3)
44
Form Handling (2/3)
45
Form Handling (3/3) • Form validation – Client side validation is faster, and will reduce server load – You should always use server side validation for security reasons if the form accesses a database
46
PHP $_GET (1/2) • The $_GET variable is used to collect values from a form with method="get" • $_GET variable is an array of variable names and values sent by the HTTP GET method • The HTTP GET method is not suitable on large variable values; the value cannot exceed 100 characters 47
PHP $_GET (2/2)
48
PHP $_POST (1/2) • Variables sent with HTTP POST are not shown in the URL • Variables have no length limit • $_POST variable is used to collect values from a form with method="post"
49
PHP $_POST (2/2)
50
PHP $_REQUEST • $_REQUEST variable can be used to get the result from form data sent with both the GET and POST method
51
Date (1/2) • formats a timestamp to a more readable date and time • date(format,timestamp)
52
Date (2/2) • Adding a timestamp • mktime(hour,minute,second,month,day,year,is_dst)
53
Include (1/4) • Server Side Includes (SSI) are used to create functions, headers, footers, or elements that will be reused on multiple pages • You can insert the content of a file into a PHP file before the server executes it, with the include() or require() function – two functions are identical in every way, except how they handle errors – Include generates a warning – Require generates a fatal error ! 54
Include (2/4) • The include() function takes all the text in a specified file and copies it into the file that uses the include function
55
Include (3/4)
56
Include (4/4) • include_once • require_once
57
File Handling (1/5) • Fopen & cie – C-like library
58
File Handling (2/5)
59
File Handling (3/5)
60
File Handling (4/5) • fclose($file) – Close an opened file
• feof() – Check end of file
• fgets(), fgetc()
61
File Handling (5/5)
62
Cookies (1/4) • A cookie is often used to identify a user • A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too • With PHP, you can both create and retrieve cookie values 63
How to Create a Cookie (2/4) • The setcookie() function is used to set a cookie – It must appear BEFORE the tag – setcookie(name, value, expire, path, domain);
64
How to Retrieve a Cookie (3/4) • $_COOKIE variable is used to retrieve a cookie value
65
How to Delete a Cookie (4/4) • When deleting a cookie you should assure that the expiration date is in the past
66
Sessions (1/4) • HTTP is stateless ! – the web server does not know who you are and what you do
• A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). • Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The UID is either stored in a cookie or is propagated in the URL
67
Sessions (2/4) • Starting a session
68
Sessions (3/4) • Storing a session variable
69
Sessions (4/4) • Destroying a session
70
Email
71
OOP (1/5) • Class definition 72
OOP (2/5) • Inheriting a class with a constructor 73
OOP (3/5) • Invocation
74
OOP (4/5) • Static method calls 75
OOP (5/5) •
Calling methods in your parent class
76
Object References (1/2) 77
Object References (2/2) 78
Object Cloning 79
Constructors and Destructors 80
Constructors and Subclasses • Subclasses should call the parent's constructor within their own constructor 81
Public, Private, Protected (1/2)
82
Public, Private, Protected (2/2) •
Object methods can be designated private, public, or protected
85
Class Constants
86
Indirect Referencing
87
Getter and Setter •
PHP 5 objects support special __get() and __set() methods to access "virtual" properties that do not actually exist
88
Abstract Classes 89
Interfaces 90
Exceptions 91
Objects as Strings 92
MySQL and PHP
93
Querying A Database • Basic steps 1. 2. 3. 4. 5.
Check and filter data coming from the user Set up a connection to the appropriate database Query a database Retrieve the results Present the results back to the user
94
Book Search Example (html) Book-O-Rama Catalog Search
Book-O-Rama Catalog Search
Choose Search Type:
Author Title ISBN
Enter Search Term:
95
results_generic.php (1/4) Book-O-Rama Search Results
Book-O-Rama Search Results