Title: PHP: Hypertext Preprocessor
1PHP Hypertext Preprocessor
- A server-side, cross-platform, HTML embedded
scripting language - Open source
- Processed by the web server and plain HTML is
returned to the browser. - Run as an external CGI process, a stand-alone
script interpreter, or an embedded Apache module.
2PHP Hypertext Preprocessor
- Database support includes MySQL, mSQL,
Informix, Oracle, Sybase, Solid, PostgreSQL,
ODBC, and more. - Some advanced features Authentication, XML,
dynamic image creation, WDDX, shared memory
support, and dynamic PDF document creation
3Hello World
- HTML file with special sections of PHP code
- Filenames of .php3 or .phtml
4Language features
- All the typical language constructs while,
do..while, for loops if..else,
switch variables, strings, arrays user defined
functions include - 700 total functions
5Page Headers Footers
- Simple use of PHP create standard header and
footer for every page on a site
html code
specific to page ?
6Browser specific HTML
- Look of document changes with browser/version
- PHP can be used tailor to the browser
"2.0")) // detects Netscape 2.0
browsers oldbrowser 1 ?html code if (oldbrowser) echo else
echo "bordercolor000000" ?html code...
7Searching a Database
- User searches a product database using a keyword
html code " METHOD"POST" NAME"sd" SIZE"20" MAXLENGTH"80"
html code...
html codepage via a form, we can refer to the INPUT POST
fields as a variable in PHP (just put a in
front of the NAME of the field) / // Build up
the SQL search query stringSelstr "SELECT
mfgpart, mfg, description, sell1 FROM products
WHERE "regdes msql_RegCase(sd) // force
case insensitive searchSelstr Selstr .
"description LIKE 'regdes ORDER BY
mfgResult MSQL(intime,Selstr) //
execute the queryResult_count
MSQL_NUM_ROWS(Result)
8Searching a Database
- if (Result_count0)
- echo "Sorry, that search didn't return
any results. Please try again.
-
- i 0 // counter variableWHILE ( i Result_count)
- // print out a line for each item in the
results - ourq0 msql_result(Result, i,
"mfgpart") ourq1 msql_result(Result, i,
"mfg") ourq2 msql_result(Result, i,
"description") - ourq3 msql_result(Result, i, "sell1")
- echo "ourq1 ourq0
ourq2
ourq3br
- i
- // end while
- ?
- html code...
9Shopping Cart
- Problem HTTP is not connection oriented
- We need to use a cookie to identify a specific
visitor
/ The first page on the site visited must set a
cookie in the users browser. From then on, that
person can be identified by a unique session ID.
The session ID is used in the database entries
related to this customers shopping cart. This
must appear in the header section of the HTML
file (before ) / / Check for a sessionid cookie, and assign
one if needed / temp1date("U")
temp2getmypid() sessionid"s"strval(
temp1)strval(temp2)strval(Rand()1000)
/ Cookie is date process number random
number / / Cookie will expire in one
day (60 sec 60 min 24 hours) /
SetCookie("sessionid",sessionid,time()606024,"
/") ?
10Shopping Cart
- Our solution based on code from www.camelcity.com
- Every HTML document includes shop.phtml ?
- shop.phtml contains cookie setting code and
functions to add items, delete items, place
order, calculate order total, and perform logging - Added shopping cart display on margin of all
pages. - Added special HIDDEN fields, quantity field, and
submit button to products listed on site.
11(No Transcript)
12Shopping Cart
- Each product gets a HTML form associated with it
- Any fields to be inserted into the database are
given as HIDDEN fields - A quantity field and submit button is added for
each product
result.phtml" METHODPOST NAMEmfg VALUE"DATAPRODUCTS" NAMEmfgp VALUE"60043" NAMEdesc VALUE"CARTRIDGE COMPATIBLE, HP
500/600 BOUNTY KIT (AND 9.00 IN
COUPONS)" VALUE"9.00" size"3" name"add"...
13Shopping Cart
- Hitting submit button causes same page to be
loaded - shop.phtml detects the event (add, delete, etc)
and calls the proper function
... if(add) Add() else if(delete)
Delete() ... function Add () global
database,sessionid,table,desc,mfg,mfgp,qty
/ Assign an order number, same user may make
multiple orders in one day / setType(temp1,"str
ing") setType(temp2,"string") temp1date("U"
) temp2getmypid() orderid"o"temp
1temp2
14Shopping Cart
// insert this product into the database sql
"insert into table values
('sessionid','orderid',qty,'mfgp','mfg','des
c) msql(database,"sql") .
..
15Advanced Logging
- Once shopping cart was in place, advanced logging
was easy - We can track each individual visitor 1) Page
the visitor arrived on 2) Subsequent pages
visited 3) Search terms used on our forms 4)
Total time spent on site 5) Did they order?
16Further Research
- www.php.net Official PHP site, software, online
docs, etc - www.hotwired.com/webmonkey/99/21/index2a.html
PHP/MySQL Tutorial - www.devshed.com/Server_Side/PHPMore tutorials,
covers more advanced topics such as creating
search engine with PHP and MySQL - Books on PHP are now becoming available, see the
official PHP site