Title: IT
1IT som værktøj
- Bent Thomsen
- Institut for Datalogi
- Aalborg Universitet
2Input
- So far we have only talked about output
- PHP can input from
- HTTP request
- Encoded in link
- Form elements
- Cookies
- Global Variables
- Files
- Databases
3HTML Forms
- HTML forms are just about the only way for PHP to
collect information from users. - Text box example
- ltForm actionthat.php methodPostgt
- ltinput typetext namevariable1gt
- ltinput typetext namevariable2gt
- ltinput typesubmit valueclick heregt
- lt/Formgt
- The above creates 2 text boxes and anything that
is type into the 1st text box will be assigned to
variable1 anything that is enter into the 2nd
textbox is assigned to variable2. These variables
are then sent to the PHP script that.php for
processing.
4In that.php
- lt?PHP
- Print(variable1, variable2 ltbrgt)
- ?gt
- that.php will output the information that is
associated with the 2 text boxes.
5Other Form Elements
- Radio Buttons
- What is your favourite pet?ltbrgt
- ltinput typeradio namefavourite_pet
valuetermitegtTermiteltbrgt - ltinput typeradio namefavourite_pet
valuecockroachgtCockroach - Checkboxes
- What magazine are you currently subscribed
to?ltbrgt - ltinput typecheckbox namet_w
valuetruegtTermite Worldltbrgt - ltinput typecheckbox namet_a
valuetruegtCockroachs Dayltbrgt - What is/are the variable(s) and the assigned
value(s)?
6Continue
- List box example
- ltselect name"hob" size"2" multiplegt
- ltoptiongtleftlt/optiongt
- ltoptiongtrightlt/optiongt
- ltoptiongttoplt/optiongt
- ltoptiongtbottomlt/optiongt
- lt/Selectgt
- Note the variable hob. In this case, an array
will be sent to a PHP script. Values can be
accessed through array indexing.
7Cookies
lt?php if (!myname) print "What is your n
ame? " print "ltFORM ACTION\"PHP_SELF\" MET
HOD\"GET\"gt\n" print "ltINPUT NAME\"myname\
" SIZE20gt\n" print "lt/FORMgt" exit
setcookie("myname", myname) ?gt
8Form Handling again
- A simple form
- ltform action"simple_form.php" method"POST"gt
- Your name ltinput typetext namenamegtltbr/gt
- You age ltinput typetext nameagegtltbr/gt
- ltinput typesubmit/gt
- lt/formgt
- The form handling code
- Hi lt?php echo name ?gt.
- You are lt?php echo age ?gt years old.
9Form Handling
- Global Variables, HTTP_GET_VARS, _GET,
register_globals configuration - Hi lt?php echo HTTP_GET_VARSname ?gt.
- You are lt?php echo _GETage ?gt years old.
10More Global Variables
Variable Name Description
DOCUMENT_ROOT Your Web server's base directory with user-visible files.
REQUEST_METHOD The HTTP method used to access this page, for example GET or POST.
REQUEST_URI Full local part of the request URL, including parameters.
HTTP_GET_VARS An associative array with the GET parameters passed to PHP, if any.
HTTP_POST_VARS An associative array with the POST parameters passed to PHP, if any.
HTTP_COOKIE _VARS An associative array with the cookies passed by the browser, if any.
SCRIPT_FILENAME File name of the top-level page being executed.
SCRIPT_NAME Local URI part of the page being executed.
SERVER_ADMIN Server administrator's email address.
SERVER_NAME Domain name for the server.
SERVER_PORT TCP port number the server runs on.
SERVER_PROTOCOL Protocol used to access the page, for example "HTTP/1.1".
11File Access
- Local File Access
- fopen, fread, fwrite, fclose, fputs, freads,
feof, much more - Remote File Access
- Uses the same functions as local file access
- Uses URLs to retrieve files, FTP and HTTP
supported. - lt?php readfile(http//www.ActiveState.com/) ?gt
- Can write files to FTP is username and password
is sent - ftp//usernamepassword_at_host.com/path/filename
12Example
lt?php visitors 0 // Initialize the
visitors to zero fr fopen('counter.txt',
'r') if(!fr) visitors 1 // Our first
visitor fr fopen('counter.txt','w')
if(!fr) echo "Could not create the
counter file!" exit fputs(fr,
visitors) fclose(fr) else
visitors fgets(fr,4096) visitors
echo "You are visitor number visitors"
fclose(fr) fr fopen('counter.txt','w'
) if(!fr) echo "Could not re-create
the counter file!"exit fputs(fr,
visitors) fclose(fr) ?gt
13Authentication
lt? function authenticate() global
PHP_AUTH_USER global PHP_AUTH_PW
if(!(PHP_AUTH_USER user" PHP_AUTH_PW
password)) Header(WWW-Authenticate
basic realmMy Website)
Header(HTTP/1.0 401 Unauthorized)
echo(Please enter a username and password to
proceed.) return false
return true if (!authenticate()) exit echo
You have authenticated properly! ?gt
14PHP and SQL Databases
- Wide range of SQL database supported
- MySQL, PostgreSQL, MS-SQL, Oracle, Sybase, ODBC,
DBM, Informix - Native interfaces (MySQL, etc), and abstracted
interfaces (ODBC, dba, PEAR) - Persistent connections supported
15MySQL
lt?php conn mysql_pconnect(localhost,
username, password) mysql_select_db(mydatabas
e, conn) res mysql_query(conn, SELECT
FROM resources) while ((rs
mysql_fetch_array(res))) echo(column1
.rs0. column2 .rs1. ltbrgt\n) mysql_
close() ?gt
16PostgreSQL
lt? // database access parameters -- alter this as
per your configuration host "localhost" user
"postgres" pass "postgres" db
"test" // open a connection to the database
server connection pg_connect ("hosthost
dbnamedb useruser passwordpass") if
(!connection) die("Could not open connection
to database server") // generate and execute
a query query "SELECT name, address FROM
addressbook ORDER BY name" result
pg_query(connection, query) or die("Error in
query query. " .pg_last_error(connection)) //
get the number of rows in the resultset // this
is PG-specific rows pg_num_rows(result)
17// if records present if (rows gt 0) //
iterate through resultset for (i0 iltrows
i) row pg_fetch_row(result,
i) ?gt ltligtltfont size"-1"gtltbgtlt? echo
row0 ?gtlt/bgtlt/fontgt ltbrgt ltfont
size"-1"gtlt? echo row1 ?gtlt/fontgt ltpgt lt?
// if no records present display
message else ?gt ltfont size"-1"gtNo data
available.lt/fontgt lt? // close database
connection pg_close(connection) ?gt
18ODBC
lt? // connect to a DSN "mydb" with a user and
password "marin" connect odbc_connect("mydb",
"marin", "marin") // query the users table for
name and surname query "SELECT name, surname
FROM users" // perform the query result
odbc_exec(connect, query) // fetch the data
from the database while(odbc_fetch_row(result))
name odbc_result(result, 1) surname
odbc_result(result, 2) print("name
surname\n") // close the connection odbc_clos
e(connect) ?gt
19Putting it all together
Web-Client
Database Server
Web-Server
HTML-Form (JavaScript)
Call PHP interpreter
WWW
DBMS
Submit Data
LAN
PHP Script
Web-Browser
SQL commands
Response
Response
Database Output
Reply
20Going Mobile with WAP
- WAP Wireless Application Protocol
- Facilitates communication between a wireless
device and a gateway, which in turn allows
communication with Internet- or intranet-based
resources - WML Wireless Markup Language
- Derivative of XML used to create pages for
wireless devices - WAP application can be built using PHP
21Compelling WAP applications
- Brief data that users want available while mobile
- Flight, directions, and traffic information
- Movie listings
- News
- Weather
- Reading email
- Controlling things house, industrial plants,
- Key today application must provide high value
with a minimum of typing - Eventually location-based services
22A Basic Card
- lt?xml version1.0?gt
- lt!DOCTYPE wml PUBLIC -//WAPFORUM//DTD WML
1.1//EN http//www.wapforum.org/DTD/wml_1.1.xml
gt - ltwmlgt
- ltcard idmain titleAn Examplegt
- ltpgt
- Hello World!
- lt/pgt
- lt/cardgt
- lt/wmlgt
23WML output from PHP
lt?php // send wml headers header("Content-type
text/vnd.wap.wml") echo "lt?xml
version\"1.0\"?gt" echo "lt!DOCTYPE wml PUBLIC
\"-//WAPFORUM//DTD WML 1.1//EN\"" . "
\"http//www.wapforum.org/DTD/wml_1.1.xml\"gt"
?gt ltwmlgt ltcard id"card1" title"Example 1"gt
ltpgt lt?php // format and output date
the_date date("M d Y") print the_date
print "ltbr/gtWelcome to a PHP-enabled site!"
?gt lt/pgt lt/cardgt lt/wmlgt
24PHP and Mobile Applications