A pache M ySQL P hp

50
Apache MySQL Php Robert Mudge Reference: http://www.w3schools.com/php/

description

A pache M ySQL P hp. Robert Mudge. Reference: http://www.w3schools.com/php/. What is Apache? www.apache.org. Apache is the de-facto standard in web servers, it is FOSS Apache powers over 70% of all web sites Apache is secure, reliable and fast - PowerPoint PPT Presentation

Transcript of A pache M ySQL P hp

Page 1: A pache    M ySQL       P hp

Apache

MySQL

Php

Robert Mudge

Reference: http://www.w3schools.com/php/

Page 2: A pache    M ySQL       P hp

What is Apache?www.apache.org

Apache is the de-facto standard in web servers, it is FOSS

Apache powers over 70% of all web sites

Apache is secure, reliable and fast Apache is easy to install and configure Apache will run on Windows, Linux and

UNIX

Page 3: A pache    M ySQL       P hp

What is PHP?www.php.net

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

Page 4: A pache    M ySQL       P hp

What is MySQL?www.mysql.com

MySQL is a small database server MySQL is ideal for small and medium

applications MySQL supports standard SQL MySQL compiles on a number of

platforms MySQL is free to download and use

Page 5: A pache    M ySQL       P hp

PHP + MySQL + Apache

PHP combined with MySQL are cross-platform (means that you can develop in Windows and serve on a Unix platform)

Page 6: A pache    M ySQL       P hp

Learning Pyramid

HTML/xHTML Scripting

Internet / Web Server / Database Server

Cascading Style Sheets

Programming C++/Java

Object Oriented Design

SQL

Program Design

PHP

Page 7: A pache    M ySQL       P hp

System Configuration Apache Server Installation

Apache/conf/httpd.conf Add module php

MySQL Server Installation my.ini passwords

PHP Installation php.ini add module for MySQL

Page 8: A pache    M ySQL       P hp

Debugging in Php

Php.ini settings Print statements Debuggers

Dbg (http://dd.cron.ru/dbg/) Gubed (http://sourceforge.net/projects/gubed/)

Summaryhttp://www-128.ibm.com/developerworks/library/os-debug/

Page 9: A pache    M ySQL       P hp

PHP Introduction PHP File HTML Header PHP script delimiter PHP Comments PHP Variables PHP Language PHP Functions/Manual

Page 10: A pache    M ySQL       P hp

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"

Page 11: A pache    M ySQL       P hp

HTML File Format

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Untitled Document</title></head>

<body></body></html>

Page 12: A pache    M ySQL       P hp

xHTML File Format

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html>

<head>

<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" /> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><meta name="Keywords“ content="beginner, mysql, php, apache” /><meta name="Description" content="web building course" /><meta http-equiv="pragma" content="no-cache" /> <meta http-equiv="cache-control" content="no-cache" /> <link rel="stylesheet" type="text/css" href=“mystyle.css" />

<title>Page Title</title>

</head> <br /><body></body>

</html>

Add Doc Type

Well FormedAll Tags open and close <tag></tag> or <tag />

Page 13: A pache    M ySQL       P hp

PHP Script Delimiters

<html>

<body>

<?php

echo "Hello World";

?>

</body>

</html>

Page 14: A pache    M ySQL       P hp

PHP Comments

<html> <body>

<?php //This is a comment /* This is a comment block */ ?>

</body> </html>

Page 15: A pache    M ySQL       P hp

PHP Variables

Variable 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 should be more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)

Page 16: A pache    M ySQL       P hp

PHP Variables + Concatenation

<html> <body>

<?php $txt1="Hello World"; $txt2="1234"; echo $txt1 . " " . $txt2 ; ?>

</body> </html>

Page 17: A pache    M ySQL       P hp

Arithmetic Operators

Page 18: A pache    M ySQL       P hp

Assignment & Comparison

Page 19: A pache    M ySQL       P hp

Logical Operators

Page 20: A pache    M ySQL       P hp

If – elseif - else

Page 21: A pache    M ySQL       P hp

switch-case-default

Page 22: A pache    M ySQL       P hp

PHP Arrays When working with PHP, sooner or later, you

might want to create many similar variables. Instead of having many similar variables, you can store the data as elements in an array.

Each element in the array has its own ID so that it can be easily accessed.

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

Page 23: A pache    M ySQL       P hp

Numeric Arrays

A numeric array stores each element with a numeric ID key.

There are different ways to create a numeric array.

Page 24: A pache    M ySQL       P hp

Numeric Array - Examples

Output: Quagmire and Joe are Peter's neighbors

Page 25: A pache    M ySQL       P hp

Associative Arrays

An associative array, each ID key is associated with a value.

When storing data about specific named values, a numerical array is not always the best way to do it.

With associative arrays we can use the values as keys and assign values to them.

Page 26: A pache    M ySQL       P hp

Associative Array - Example

Example 1 In this example we use an array to

assign ages to the different persons:

Page 27: A pache    M ySQL       P hp

Associative Array - Examples

Output: Peter is 32 years old.

Page 28: A pache    M ySQL       P hp

Multi-dimensional Arrays

In a multidimensional array, each element in the main array can also be an array.

Each element in the sub-array can be an array, and so on.

Page 29: A pache    M ySQL       P hp

Multi-dim Array Example

Page 30: A pache    M ySQL       P hp

Repetition and Looping Very often when you write code, you want the same

block of code to run a number of times. You can use looping statements in your code to perform this.

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

Page 31: A pache    M ySQL       P hp

while loopZero to n loops

Page 32: A pache    M ySQL       P hp

do – while LoopAt least once + n loops

Page 33: A pache    M ySQL       P hp

for loop - repetition The for statement is used when you know how many

times you want to execute a statement or a list of statements.

Note: The for statement has three parameters. The first

parameter initializes variables, the second parameter holds the condition, and the third parameter contains the increments required to implement the loop. If more than one variable is included in the initialization or the increment parameter, they should be separated by commas. The condition must evaluate to true or false.

Page 34: A pache    M ySQL       P hp

for loop - example

Page 35: A pache    M ySQL       P hp

foreach loop

The foreach statement is used to loop through arrays.

For every loop, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop, you'll be looking at the next element.

Page 36: A pache    M ySQL       P hp

foreach - Example

Page 37: A pache    M ySQL       P hp

Php Functions

A function is a block of code that can be executed whenever we need it.

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

Page 38: A pache    M ySQL       P hp

Function Example

<html> <body>

<?php function writeMyName() { echo “John Smith"; }

writeMyName();

?> </body> </html>

Page 39: A pache    M ySQL       P hp

Function Parameters

<html> <body>

<?php function writeName($first, $second) { echo $first . “ $second” . ‘<br>’; }

writeName(‘John’, ‘Smith’);

?> </body> </html>

Page 40: A pache    M ySQL       P hp

Function Returns

<html> <body>

<?php function getName($first, $second) { $name = $first . “ $second” . ‘<br>’; return $name; }

getName(‘John’, ‘Smith’);

?> </body> </html>

Page 41: A pache    M ySQL       P hp

Forms and User Input

The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.

The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.

print_r($_POST); echo $_SERVER['PHP_SELF'];

Page 42: A pache    M ySQL       P hp

Forms Example

welcome.php

input.php

Page 43: A pache    M ySQL       P hp

Form Validation

User input should be validated on the browser whenever possible (by client scripts (JavaScript)). Browser validation is faster and you reduce the server load.

You should consider using server validation if the user input will be inserted into a database.

A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.

Page 44: A pache    M ySQL       P hp

Php $_GET

The $_GET variable is used to collect values from a form with method="get".

The $_GET variable is an array of variable names and values sent by the HTTP GET method.

The $_GET variable is used to collect values from a form with method="get". Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and it has limits on the amount of information to send (max. 100 characters).

Page 45: A pache    M ySQL       P hp

Php $_GET Example

input.php

welcome.php

http://localhost/welcome.php?name=Peter&age=37

Page 46: A pache    M ySQL       P hp

Php $_GET Considerations

When using the $_GET variable all variable names and values are displayed in the URL.

This method should not be used when sending passwords or other sensitive information.

The variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

The HTTP GET method is not suitable on large variable values; the value cannot exceed 100 characters.

Page 47: A pache    M ySQL       P hp

Php $_REQUEST

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.

The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

Page 48: A pache    M ySQL       P hp

Php $_POST

$_POST variable is used to collect values from a form with method="post".

The $_POST variable is an array of variable names and values sent by the HTTP POST method.

The $_POST variable is used to collect values from a form with method="post". Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

Page 49: A pache    M ySQL       P hp

Php $_POST Example

welcome.php

input.php

Page 50: A pache    M ySQL       P hp

Homework #2

Create a Login Web Page User enters “username” and “password” Upon successful input and comparison

to a hard coded password, present a welcome page showing the username and password

Turn in your source code and screen captures from the operation through a web server.