Download presentation
Presentation is loading. Please wait.
Published byBarry Clyde Casey Modified over 9 years ago
1
PHP - Hypertext Preprocessor
2
Introduction PHP is a powerful server-side scripting language for creating dynamic and interactive websites. PHP is a powerful server-side scripting language for creating dynamic and interactive websites. PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP. PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP. PHP is perfectly suited for Web development and can be embedded directly into the HTML code. PHP is perfectly suited for Web development and can be embedded directly into the HTML code. The PHP syntax is very similar to Perl and C. The PHP syntax is very similar to Perl and C. PHP is often used together with Apache (web server) on various operating systems. PHP is often used together with Apache (web server) on various operating systems. It also supports ISAPI and can be used with Microsoft's IIS on Windows. It also supports ISAPI and can be used with Microsoft's IIS on Windows.
3
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP is a server-side scripting language, like ASP PHP scripts are executed on the server even though even though combined with an HTML code PHP scripts are executed on the server even though even though combined with an HTML code PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software (OSS) PHP is an open source software (OSS) PHP is free to download and use PHP is free to download and use
4
What is a PHP File? PHP files may contain text, HTML tags and scripts PHP files may contain text, HTML tags and scripts PHP files are returned to the browser as plain HTML PHP files are returned to the browser as plain HTML PHP files have a file extension of ".php", ".php3", or ".phtml" PHP files have a file extension of ".php", ".php3", or ".phtml"
5
What is MySQL? MySQL is a database server MySQL is a database server MySQL is ideal for both small and large applications MySQL is ideal for both small and large applications MySQL supports standard SQL MySQL supports standard SQL MySQL compiles on a number of platforms MySQL compiles on a number of platforms MySQL is free to download and use MySQL is free to download and use
6
PHP + MySQL PHP combined with MySQL are cross-platform (means that you can develop in Windows and serve on a Unix platform) PHP combined with MySQL are cross-platform (means that you can develop in Windows and serve on a Unix platform)
7
Why PHP? PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource: www.php.net PHP is FREE to download from the official PHP resource: www.php.netwww.php.net PHP is easy to learn and runs efficiently on the server side PHP is easy to learn and runs efficiently on the server side
8
Where to Start? Install an Apache server on a Windows or Linux machine Install an Apache server on a Windows or Linux machine Install PHP on a Windows or Linux machine Install PHP on a Windows or Linux machine Install MySQL on a Windows or Linux machine Install MySQL on a Windows or Linux machine Install PHP triad Install PHP triad All in one All in one LAMP LAMP Linux Apache MySQL PHP Linux Apache MySQL PHP WAMP (Apache, PHP, MySQL) WAMP (Apache, PHP, MySQL) http://www.wampserver.com/en/ http://www.wampserver.com/en/ http://www.wampserver.com/en/ XAMPP (MySQL, PHP and Perl) XAMPP (MySQL, PHP and Perl) http://www.apachefriends.org/en/xampp.html http://www.apachefriends.org/en/xampp.html http://www.apachefriends.org/en/xampp.html
9
First Example – Hello World! Activity 01 Activity 01 NOTES NOTES You cannot view the PHP source code by selecting "View source" in the browser You cannot view the PHP source code by selecting "View source" in the browser you will only see the output from the PHP file, which is plain HTML. you will only see the output from the PHP file, which is plain HTML. This is because the scripts are executed on the server before the result is sent back to the browser. This is because the scripts are executed on the server before the result is sent back to the browser.
10
PHP Syntax A PHP scripting block always starts with. A PHP scripting block always starts with. A PHP scripting block can be placed anywhere in the HTML document. A PHP scripting block can be placed anywhere in the HTML document. On servers with shorthand support enabled you can start a scripting block with. On servers with shorthand support enabled you can start a scripting block with. However, for maximum compatibility, it is recommend that you use the standard form (<?php) rather than the shorthand form. However, for maximum compatibility, it is recommend that you use the standard form (<?php) rather than the shorthand form.
11
PHP Syntax A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code. A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code. View source for activity 01 View source for activity 01 Each code line in PHP must end with a semicolon. 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. The semicolon is a separator and is used to distinguish one set of instructions from another. There are two basic statements to output text with PHP: echo and print. There are two basic statements to output text with PHP: echo and print. In activity 01, we have used the echo statement to output the text "Hello World". In activity 01, we have used the echo statement to output the text "Hello World".
12
PHP Syntax Comments in PHP Comments in PHP In PHP, we use // to make a single-line comment or In PHP, we use // to make a single-line comment or /* and */ to make a large comment block. /* and */ to make a large comment block.
13
PHP Variables All variables in PHP start with a $ sign symbol. All variables in PHP start with a $ sign symbol. Variables may contain strings, numbers, or arrays (untype). Variables may contain strings, numbers, or arrays (untype). Activity 02 Activity 02 Variable concatenation – using the (.) operator Variable concatenation – using the (.) operator All converted to string All converted to string Str. Str = str Str. Str = str Num. Num = str Num. Num = str
14
Variable Naming Rules Same as C, C++ or Java Same as C, C++ or Java A variable name must start with a letter or an underscore "_" 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 can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ ) A variable name should not contain spaces. 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), If a variable name should be more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString) or with capitalization ($myString)
15
PHP operator, conditional Same as C or C++ Same as C or C++ PHP If...Else Statements – Activity 03 PHP If...Else Statements – Activity 03 Same as C or C++ Same as C or C++ Swith statement Swith statement Same as C or C++ Same as C or C++
17
PHP array (Numeric) Numeric Arrays ( Activity 04 ) Numeric Arrays ( Activity 04 ) $names = array("Peter","Quagmire","Joe"); $names = array("Peter","Quagmire","Joe"); Assign manualy: Assign manualy: $names[0] = "Peter"; $names[0] = "Peter"; $names[1] = "Quagmire"; $names[1] = "Quagmire"; $names[2] = "Joe"; $names[2] = "Joe";
18
PHP array (Associative) Using Hash technique ( Activity 04 ) Using Hash technique ( Activity 04 ) An associative array where each ID key is associated with a value. An associative array where 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. 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. With associative arrays we can use the values as keys and assign values to them.
19
PHP array (Associative) $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); or or $ages['Peter'] = "32"; $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; $ages['Joe'] = "34";
21
Tutorial 01 Download tutorial01.php Download tutorial01.php Produce out such as the following: Produce out such as the following: Father’s name: Quagmire Father’s name: Quagmire Children’s name: Glenn Children’s name: Glenn You need to upload your tutorial01.php to the server to view the result! You need to upload your tutorial01.php to the server to view the result!
22
PHP Looping – Activity 05 Same as C or C++ Same as C or C++ while - loops through a block of code if and as long as a specified condition is true 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 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 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 foreach - loops through a block of code for each element in an array
23
PHP – Data transfer Transferring data between client (web browser) to the server-side Transferring data between client (web browser) to the server-side Two method: Two method: Using form: POST method Using form: POST method Through URL: GET method Through URL: GET method
24
POST Method The body of the message is sent as a stream of data (HTML form data) Separated with the PHP URL in the FORM post URL Client send data to servlet using HTML form element
25
POST Method Form tag <FORM METHOD=”post” ACTION=”login.php” ACTION=”login.php” TARGET=“”> TARGET=“”> Fill the TARGET value if form result have to display in a different frame After coding all the form element (button, textfield, etc) FORM tag must be close using the equivalent end tag - After coding all the form element (button, textfield, etc) FORM tag must be close using the equivalent end tag - If you have multiple form in a single page every separate every form using the end tag
26
GET method The body of the message (the data) is appended to the PHP URL, http://myserver.com/hello.php Separated by a question mark http://myserver.com/hello.php? Followed by name-value pair which separated by equals sign If value consist of more than one word, separate it using plus sign which the php will convert it to space character after parsing name=john+doe Every consecutive name-value pair will be separated using ampersand sign (&) name=john+doe&id=007
27
PHP Function Also similar to C, C++ and Java Normal – 8-1 With parameter – 8-2 Return value – 8-3
28
PHP object
31
PHP MySQL – Connect/Disconnect
32
PHP MySQL – mysql_query() mysql_query ( string $query [, resource $link_identifier] ) $query A SQL query The query string should not end with a semicolon. $link_identifier DB connection If is not specified, the last link opened by mysql_connect() is assumed otherwise DB error mysql_connect()
33
PHP MySQL – Create (Db & Table)
40
PHP MySQL – INSERT
42
PHP MySQL – SELECT
44
The example above stores the data returned by the mysql_query() function in the $result variable. Next, we use the mysql_fetch_array() function to return the first row from the recordset as an array. Each subsequent call to mysql_fetch_array() returns the next row in the recordset. The while loop loops through all the records in the recordset. To print the value of each row, we use the PHP $row variable ($row['FirstName'] and $row['LastName']).
45
PHP MySQL – SELECT WHERE clause WHERE clause
46
PHP MySQL – SELECT ORDER by – record sorting ORDER by – record sorting
47
PHP MySQL – UPDATE
48
PHP MySQL – DELETE
49
PHP Session A PHP session allow us to store user information on the server for later use (i.e. username, shopping items, etc). A PHP session allow us to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website. However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to store the data in a database. If you need a permanent storage you may want to store the data in a database. Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. 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. The UID is either stored in a cookie or is propagated in the URL.
50
PHP Cookies A cookies allow us to store user information permanently on the user’s machine (client) for later use (i.e. username & password, last visit etc). A cookies allow us to store user information permanently on the user’s machine (client) for later use (i.e. username & password, last visit etc). arguments are setcookie(name, value, expiration): arguments are setcookie(name, value, expiration):
51
PHP Cookies A cookies allow us to store user information permanently on the user’s machine (client) for later use (i.e. username & password, last visit etc). A cookies allow us to store user information permanently on the user’s machine (client) for later use (i.e. username & password, last visit etc). arguments are setcookie(name, value, expiration): arguments are setcookie(name, value, expiration):
52
PHP Cookies name: The name of your cookie. You will use this name to later retrieve your cookie, so don't forget it! name: The name of your cookie. You will use this name to later retrieve your cookie, so don't forget it! value: The value that is stored in your cookie. Common values are username(string) and last visit(date). value: The value that is stored in your cookie. Common values are username(string) and last visit(date). expiration: The date when the cookie will expire and be deleted. If you do not set this expiration date, then it will be treated as a session cookie and be removed when the browser is restarted. expiration: The date when the cookie will expire and be deleted. If you do not set this expiration date, then it will be treated as a session cookie and be removed when the browser is restarted. setcookie("user", “kim bo-ra", time()+3600); setcookie("user", “kim bo-ra", time()+3600); Expire in 1 hour Expire in 1 hour
53
PHP Cookies Last visit: Last visit: //Calculate 60 days in the future //Calculate 60 days in the future //seconds * minutes * hours * days + current time //seconds * minutes * hours * days + current time $inTwoMonths = 60 * 60 * 24 * 60 + time(); $inTwoMonths = 60 * 60 * 24 * 60 + time(); setcookie(lastVisit, date("G:i - m/d/y“, $inTwoMonths) setcookie(lastVisit, date("G:i - m/d/y“, $inTwoMonths) Deleting cookies: Deleting cookies: When deleting a cookie you should assure that the expiration date is in the past. When deleting a cookie you should assure that the expiration date is in the past. setcookie("user", "", time()-3600); setcookie("user", "", time()-3600);
54
PHP Session Starting session: Starting session: session_start(); session_start(); No session exist – new session variable created, No session exist – new session variable created, otherwise, current session variable is retrieve otherwise, current session variable is retrieve Storing data in session ($_SESSION) Storing data in session ($_SESSION) $_SESSION['views']=1; $_SESSION['views']=1; Retrieving and checking Retrieving and checking echo "Pageviews=". $_SESSION['views']; echo "Pageviews=". $_SESSION['views']; isset($_SESSION['user']) – checking if variable user exist in $_SESSION isset($_SESSION['user']) – checking if variable user exist in $_SESSION Destroying session data and session Destroying session data and session Session data: unset($_SESSION['views']); Session data: unset($_SESSION['views']); session_destroy(); session_destroy();
55
Authenticating, Access Control & Profile Management Using FORM authentication Supplying login and password through HTML Form to log to the restricted application Data send to php script using SSL protocol – prevent from sniffer PHP for login-password processing and PHP redirecting Authenticating user login and password from the database Creating user session Creating user profile using User Object and store in the user newly created session Direct user to the session protected PHP pages
56
Authenticating, Access Control & Profile Management PHP pages (view) Control user access to protected resources using user session Every PHP pages which involve in the restricted application should also have a section for session authentication
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.