PHP Outline What is PHP What can PHP

  • Slides: 26
Download presentation
PHP

PHP

Outline § § § § § What is PHP? What can PHP do? PHP

Outline § § § § § What is PHP? What can PHP do? PHP Basics PHP and Forms Cookies and Sessions Database Connections Command Line PHP Other PHP capabilities Useful Links Slide # 2 www. venturenext. com

What is PHP? ? § § § PHP: Hypertext Preprocessor PHP is a scripting

What is PHP? ? § § § PHP: Hypertext Preprocessor PHP is a scripting language that allows you to create dynamic web pages You can embed PHP scripting within normal html coding PHP was designed primarily for the web PHP includes a comprehensive set of database access functions Slide # 3 www. venturenext. com

What can PHP do? § § Server side scripting CGI Server module Command line

What can PHP do? § § Server side scripting CGI Server module Command line scripting GUI applications Some of the protocols PHP speaks: LDAP, IMAP, SNMP, NNTP, POP 3, HTTP, CORBA, XML(SAX/DOM), Perl regular expressions, Cybercash payment, Cyber. MUT, Veri. Sign Payflow Pro, and most relational databases Slide # 4 www. venturenext. com

PHP Basics PHP echos everything to standard output until it comes across special tags:

PHP Basics PHP echos everything to standard output until it comes across special tags: <? ? >, <% %>, <? php ? > <? if ($expression) { ? > <strong>This is true. </strong> <? } else { ? > <strong>This is false. </strong> <? } ? > Slide # 5 www. venturenext. com

PHP Basics (contd. ) § § § Comments: § // /* */ # Variable

PHP Basics (contd. ) § § § Comments: § // /* */ # Variable Types § boolean $foo = True; § integer $a=1234; $a=0123; $a=0 x 1 A; § float $a=1. 234; $a=1. 2 e 3; $a=7 E-10; § string $name = 'My. Name'; § array $arr[key] = value; § object Variables § § § § All variables begin with $ and can contain letters, digits and underscore (and no digit directly after the $) The value of a variable is the value of its most recent assignment Don’t need to declare variables Variables have no intrinsic type other than the type of their current value Can have variables $$variable Like a pointer variable type; best to avoid Slide # 6 www. venturenext. com

Predefined Variables § § § § $GLOBALS $_SERVER $_GET $_POST $_COOKIE $_FILE $_REQUEST $_SESSION

Predefined Variables § § § § $GLOBALS $_SERVER $_GET $_POST $_COOKIE $_FILE $_REQUEST $_SESSION Slide # 7 www. venturenext. com

Variable scope § § § The scope of a variable is the context within

Variable scope § § § The scope of a variable is the context within which it is defined. Within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. This is circumvented by using the “global” or “static” declaration: function Sum() { global $a, $b; $b = $a + $b; } Slide # 8 www. venturenext. com

Control Strctures § § § § if elseif while do. . while foreach break

Control Strctures § § § § if elseif while do. . while foreach break continue switch declare return require() include() require_once() include_once() Slide # 9 www. venturenext. com

Functions Default argument values § function foo ($arg_1="value", $arg_2=7. 3, . . . ,

Functions Default argument values § function foo ($arg_1="value", $arg_2=7. 3, . . . , $arg_n) Variable-length argument lists § func_num_args() § func_get_arg() function foo ($arg_1, $arg_2, . . . , $arg_n) { echo "Example function. n"; return $retval; } function square ($num) { return $num * $num; } echo square (4); // outputs '16' Slide # 10 www. venturenext. com

Objects <? php class Cart { var $items; // Items in our shopping cart

Objects <? php class Cart { var $items; // Items in our shopping cart function Cart() { $this->todays_date = date("Y-m-d"); $this->name = $GLOBALS['firstname']; /* etc. . . */ } // Add $num articles of $artnr to the cart function add_item ($artnr, $num) { $this->items[$artnr] += $num; } // Take $num articles of $artnr out of the cart function remove_item ($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } else { return false; } } } ? > Slide # 11 www. venturenext. com

PHP and Forms An example HTML form: <form action="array. php" method="post"> Name: <input type="text"

PHP and Forms An example HTML form: <form action="array. php" method="post"> Name: <input type="text" name="personal[name]"> Email: <input type="text" name="personal[email]"> Username: <input type="text" name="username"> Beer: <select multiple name="beer[]"> <option value="warthog">Warthog <option value="guinness">Guinness <option value="stuttgarter">Stuttgarter </select> <input type="submit"> </form> Slide # 12 www. venturenext. com

PHP and Forms (contd. ) Variables accessible in array. php: § $_POST[“personal”][“name”] § $_POST[“personal”][“email”]

PHP and Forms (contd. ) Variables accessible in array. php: § $_POST[“personal”][“name”] § $_POST[“personal”][“email”] § $_POST[“beer”] § $_POST[“username”] For more on HTML forms: http: //www. w 3 schools. com/html_forms. asp? output=print Slide # 13 www. venturenext. com

PHP and Forms (contd. ) Passing variables from page to page: $username from array.

PHP and Forms (contd. ) Passing variables from page to page: $username from array. php will be available for array 2. php Array. php: <form action="array 2. php" method="post"> <input type=“hidden” name=“username” value=“<? echo “username”; ? >” Favorite Color: <input type="text" name=”color"> <input type="submit"> </form> Slide # 14 www. venturenext. com

A form processing example <? while($temp = each($_POST)) { echo "Variable $temp[‘key’] contains ”

A form processing example <? while($temp = each($_POST)) { echo "Variable $temp[‘key’] contains ” echo “<strong>$temp[‘value’]</strong> "; } ? > Slide # 15 www. venturenext. com

GET vs. POST § GET § Accessed through the $_GET variable § Puts variables

GET vs. POST § GET § Accessed through the $_GET variable § Puts variables in the URL § Doesn’t require user to click “reload” if they revisit a page with the “back” button § Good for passing small amounts of data § POST § Accessed through the $_POST variable § Passes variables in Content HTTP directive … not in URL § If a user clicks the “back” button, the browser will ask the user to reload the page § Good for passing large amounts of data, but may cause navigation problems Slide # 16 www. venturenext. com

Cookies are useful for storing user info that should be retained from one page

Cookies are useful for storing user info that should be retained from one page to the next. (Overcome the ‘stateless’ nature of the web) Cookies are written to the client’s hard drive. Problems: § User can disable cookies in the browser § Cookies may be viewed by other users § Can only store 20 cookies; max 4 KB. § Some browsers may display incorrectly unless all options are set in setcookie() Slide # 17 www. venturenext. com

Creating a Cookie setcookie(name, value, expiration); Eg, setcookie(“fruit”, ”banana”, time()+3600); The cookies is called

Creating a Cookie setcookie(name, value, expiration); Eg, setcookie(“fruit”, ”banana”, time()+3600); The cookies is called ‘fruit’ and has a value of ‘banana’; it will expire 1 hr from now. Cookie values are sent as part of the HTTP headers (transparent to user). No output should be sent to the browser (echo etc) until the cookie is set else cookie will not be set. Slide # 18 www. venturenext. com

Cookie (contd. ) Accessing A Cookie § $_COOKIE contains the value of every current

Cookie (contd. ) Accessing A Cookie § $_COOKIE contains the value of every current cookie <? foreach ($_COOKIE as $name =>$value) echo “<BR>$name => $value”; ? > Deleting a Cookie § Automatically deleted after expiration time § You can manually delete by setting negative time § setcookie(“username”, ””, time()-3600); Slide # 19 www. venturenext. com

Redirection Once login data is captured/validated then want to go to a new page.

Redirection Once login data is captured/validated then want to go to a new page. § Header(“Location: URL”); header("Location: http: //capb. dbi. udel. edu/mypage/main. php"); General technique: § Site start page = login page § Login page validates user and set cookies § Redirect to new page § New page uses cookie data to access DB info Slide # 20 www. venturenext. com

Sessions § § § Used to store state across pages To save a variable

Sessions § § § Used to store state across pages To save a variable simply set it in the $_SESSION variable: § $_SESSION[“username”] = $username; To access a saved variable, access the $_SESSION variable: § $username = $_SESSION[“username”]; To remove a variable, unset the $_SESSION variable: § unset($_SESSION[“username”]); http: //www. php. net/manual/en/ref. session. php Slide # 21 www. venturenext. com

Databases and PHP § § § PHP can access many types of RDBMS’s My.

Databases and PHP § § § PHP can access many types of RDBMS’s My. SQL, M$SQL, Oracle, Sybase, M$ Access, d. Base, dbm, Postgre. SQL Use database independent interfaces § My inc. db. php script § DB. php in PEAR § http: //pear. php. net/manual/en/core. db. php ADODB § http: //php. weblogs. com/ADODB Slide # 22 www. venturenext. com

Command Line PHP § § What is this good for: § Parsing files to

Command Line PHP § § What is this good for: § Parsing files to put into a database § Updating files with latest public version (cron job) § Anything you’d use a shell script or perl script to do! Variables of use: § $argc, $argv[] § $stdin, $stdout, $stderr Slide # 23 www. venturenext. com

Other PHP capabilities § § § § § IMAP XML parsing FTP IRC Java

Other PHP capabilities § § § § § IMAP XML parsing FTP IRC Java LDAP Mail Ncurses Open. SSL Regular Expressions Compression PDF file generation Shockwave Flash COM and. NET objects Password strength tests CURL Ecommerce protocols Slide # 24 www. venturenext. com

Useful Links § § § PHP main site: § http: //www. php. net Zend’s

Useful Links § § § PHP main site: § http: //www. php. net Zend’s PHP Intro: § http: //www. zend. com/zend/art/intro. php PHPBuilder: § http: //www. phpbuilder. com/ Slide # 25 www. venturenext. com

Thank You Slide # 26 www. venturenext. com

Thank You Slide # 26 www. venturenext. com