4.Presentation

advertisement
‫הרצאה ‪4‬‬
‫עיבוד של דף אינטרנט דינמי‬
‫דף אינטרנט דינמי משתנה עפ"י הרצת קוד על‬
‫ יכול להשתנות בכל עדכון‬,‫השרת‬

HTTP request
`
Web Browser
HTTP response
Web Server
Database Server
PHP
Script
.Murach’s PHP and MySQL by Joel Murach and Ray Harris ‫מתוך‬
‫תהליך העיבוד של דף אינטרנט דינמי‬
• The web browser builds an HTTP request, and sends it to the relevant
web server.
• If the user submitted data, it will be included in the request.
• The web server checks the file extension of the requested page. If it is
PHP it invokes the PHP interpreter which is running on the web server.
• The PHP interpreter finds the relevant PHP script from the hard drive,
along with any data it needs, and executes the script (it may request
data from the database).
• The script generates an HTML page as output, and sends it to the client
as an HTTP response.
• The response includes the HTML formatting for the page.
• The web browser (client) receives the HTTP response, and uses the HTML
to format and display the page in the web browser.
• The browser does not know if the page was obtained from a static HTML
page or generated using PHP.
PHP


PHP: Hypertext Preprocessor
What is a PHP File?
PHP files can contain text, HTML, CSS, JavaScript, and PHP
code
 PHP code are executed on the server, and the result is
returned to the browser as plain HTML
 PHP files have extension ".php"


Example:
<?php
echo "My first PHP script!";
?>

Installed as part of WAMP
What Can PHP Do?







Generate dynamic page content
Create, open, read, write, delete, and close files on
the server
Collect form data
Add, delete, modify data in your database
Can be used to control user-access
PHP can encrypt data
Send and receive cookies
PHP Syntax








A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>:
The default file extension for PHP files is ".php".
A PHP file normally contains HTML tags, and some PHP
scripting code.
In PHP, all keywords (e.g. if, else, while, echo, etc.), classes,
functions, and user-defined functions are NOT case-sensitive.
However; all variable names are case-sensitive.
In PHP, a variable starts with the $ sign, followed by the
name of the variable.
http://www.w3schools.com/php/default.asp
Output to HTML: Echo / print

Echos what is in ” ” to the html.
 Any

html can be input…
Examples
 echo
”hi”;
 echo ”<br>”;
 echo”<h1>blabla</h1>”;
 echo “<script>alert(‘careful’)</script>;

There is also a print function:
 print
"Hello world!<br>";
Data Types


Variables can store data of different types, they are
not declared.
Strings (in single or double quotes)
<?php
$x = "Hello world!";
$y = 'Hello world!';
?>

Integers
$x = 5985;

For other types:
http://www.w3schools.com/php/php_datatypes.asp
Conditional Statements




http://www.w3schools.com/php/php_if_else.asp
http://www.w3schools.com/php/php_switch.asp
http://www.w3schools.com/php/php_looping.asp
http://www.w3schools.com/php/php_looping_for.a
sp
Functions

Aside from built in functions, we can write our own:
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
?>
Arrays

Indexed array:
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>

Associative array (use named keys that you assign to
them):
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
SuperGlobals

Several predefined variables in PHP are "superglobals“



They are always accessible, regardless of scope
You can access them from any function, class or file without having to do
anything special.
The PHP superglobal variables are:









$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
$_GET

PHP superglobal $_GET is used to collect form data.

$_POST is also a superglobal used to collect form data


We will learn it later
The file welcome.php:
Form Validation


http://www.w3schools.com/php/php_form_validati
on.asp
Self reading…
Working With DB



With PHP, you can connect to and manipulate
databases.
Some examples will be given in class document
Others can be found here
 http://www.w3schools.com/php/php_mysql_intro.asp

And other places…
Download