COMP519 Web Programming
Lecture 23: PHP (Part 5)
Handouts
Ullrich Hustadt
Department of Computer Science
School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
1 Web Applications
2 Available Information and Input
3 Further Reading
COMP519 Web Programming Lecture 23 Slide L23 1
Web Applications Overview
Web Applications using PHP
IBM: Build Ajax-based Web sites with PHP, 2 Sep 2008.
https://www.ibm.com/developerworks/library/wa-aj-php/ [accessed 6 Mar 2013]
COMP519 Web Programming Lecture 23 Slide L23 2
Web Applications HTML Forms
HTML Forms
When considering Python CGI programming we have used HTML forms
that generated a client request that was handled by a
Python CGI program:
< form action =
" http :// student . csc .liv . ac . uk / cgi - bin / cgiwrap /uh / demo "
method = " post ">
...
</ form >
Now we will use a PHP script instead:
< form action = " http :// st udent . csc . liv . ac . uk /
uh / demo . php "
method = " post " >
...
</ form >
The PHP script file must be stored in a directory accessible by the web
server, for example $HOME/public_html, and be readable by the web
server
The PHP script file name must have the extension .php, e.g. demo.php
COMP519 Web Programming Lecture 23 Slide L23 3
Available Information and Input Overview
Information Available to PHP Scripts
Information on the PHP environment
Information on the web server and client request
Form data
Cookie/Session data
Miscellaneous
string date(format)
returns the current date/time presented according to format
for example, date('H:i l, j F Y')
results in 12:20 Thursday, 8 March 2012
(See http://www.php.net/manual/en/function.date.php)
int time()
returns the current time measured in the number of seconds
since January 1 1970 00:00:00 GMT
COMP519 Web Programming Lecture 23 Slide L23 4
Available Information and Input PHP Environment
PHP Environment
phpinfo() displays information about the PHP installation and
EGPCS data (Environment, GET, POST, Cookie, and Server data)
for the current client request
phpinfo(part) displays selected information
< html lang = "en - GB " >< head ></ head > < body >
<? php
phpi nfo (); // Show all in for mat ion
phpi nfo ( IN FO_ VA RIA BL ES ); // Show only info on EGPCS data
?>
</ body > </ html >
http://cgi.csc.liv.ac.uk/
~
ullrich/COMP284/examples/phpinfo.php
INFO_GENERAL The configuration, php.ini location, build date,
web server
INFO_CONFIGURATION Local and master values for PHP directives
INFO_MODULES Loaded modules
INFO_VARIABLES All EGPCS data
COMP519 Web Programming Lecture 23 Slide L23 5
Available Information and Input PHP Environment
Manipulating the PHP Configuration
The following functions can be used to access and change the
configuation of PHP from within a PHP script:
array ini_get_all()
returns all the registered configuration options
string ini_get(option)
returns the value of the configuration option on success
string ini_set(option, value)
sets the value of the given configuration option to a new value
the configuration option will keep this new value during the script’s
execution and will be restored afterwards
void ini_restore(option)
restores a given configuration option to its original value
COMP519 Web Programming Lecture 23 Slide L23 6
Available Information and Input Server Variables
Server Variables
The superglobal $_SERVER array stores information about the web server
and the client request
; Similar to os.environ for Python CGI programs
< html lang = "en - GB " >< head ></ head > < body >
<? php
echo ' Se rver s oftware : ' , $_SE RVER [ ' SE RVER_ SOFTW AR E ' ] , ' <br > ';
echo ' Remote a ddress : ' , $_SE RVER [ ' REM OTE_A DDR '] , ' <br > ';
echo ' Client b rowser : ' , $_SE RVER [ ' HT TP_US ER_AG EN T ' ] , ' <br > ';
echo ' R equest meth od : ' , $_SE RVER [ ' RE QUE ST _ME TH OD ' ];
? > </ body ></ html >
http://cgi.csc.liv.ac.uk/
~
ullrich/COMP284/examples/server.php
Server sof twar e : Apache / 2.2.22 ( Fedor a )
Remote addr ess : 1 0.1 28. 0.215
Client brow ser : Mozilla /5.0 ... Chrome /4 1.0.2 272 .5 3 ...
Request me thod :
See http://php.net/manual/en/reserved.variables.server.php
for a list of keys
COMP519 Web Programming Lecture 23 Slide L23 7
Available Information and Input Form Data
Form Data
Form data is passed to a PHP script via the three superglobal arrays:
$_POST Data from POST client requests
$_GET Data from GET client requests
$_REQUEST Combined data from POST and GET client requests
(derived from $_POST and $_GET)
; Accessing $_REQUEST is the equivalent in PHP to
accessing the ‘dictionary’ of a cgi.FieldStorage instance in
Python
< form action = " process . php " me thod =" post " >
<label > Enter your user name :
< input type =" text " name =" usern ame " > </ label >< br >
<label > Enter your full name :
< input type =" text " name =" fulln ame " > </ label >< br >
< input type =" submit " value =" Click for r espo nse " > </ form >
$_REQUEST['username'] Value entered into field with name ‘username’
$_REQUEST['fullname'] Value entered into field with name ‘fullname’
COMP519 Web Programming Lecture 23 Slide L23 8
Available Information and Input Form Data
Forms in PHP: Example (1)
Create a web-based system that asks the user to enter the URL of a file
containing bibliographic information
Bibliographic informatiom will have the following form:
@entry {
name ={ Jonas Lehn er } ,
name ={ Andr eas Sch okn ech t } ,
title ={ < strong > You only live twice </ strong >} ,
}
@entry {
name ={ Andr eas Sch okn ech t } ,
name ={ Eva Egg elin g } ,
title ={ No End in Sight ?} ,
}
The system should extract the names, count them, and create a table of
names and their frequency, ordered from most frequent to least frequent
COMP519 Web Programming Lecture 23 Slide L23 9
Available Information and Input Form Data
Forms in PHP: Example (1)
Useful PHP functions:
string file_get_contents(filename)
returns the contents of the file/URL filename, or FALSE on failure
array array_count_values(arr)
returns an array using the values of the array arr as keys and their
frequency in arr as values
$array = array ( 'a ' ,' c ' , 'b ' ,' b ' , 'c ' , ' c ' );
$count = arra y_ co unt_v al ue s ( $ a rray );
# $count = [' a ' => 1 , ' c ' => 3, ' b ' = > 2]
bool arsort(arr)
sorts arr according to associated values maintaining their correlation
with keys, returns TRUE on success and FALSE on failure
arsort ( $ cou nt )
# $count = [' c ' => 3 , 'b ' => 2 , ' a ' = > 1]
COMP519 Web Programming Lecture 23 Slide L23 10
Available Information and Input Form Data
Forms in PHP: Example (1)
extract_names.php
<! DOC TY PE html >
<html >< head >< title > N ame Ex tr ac tion </ title > </ head > < body >
<? php
re qui r e_o nce ' ex tra cti on . php ' ;
if ( is se t ( $ _SE RV ER [ ' RE QUE S T_M ETH O D ' ]) &&
$_ SER VE R [ ' REQ UES T _ME THO D '] == ' POST ' &&
isset ( $ _R EQU EST [ ' url ' ])) {
$ e x tra c ted _ nam e s = ex tra c t_n ame s ( $ _ REQ UES T [ ' url ' ]);
ech o "<div > The na mes o ccu rri ng in <br > " , html spe c ial c har s ( $ _ R EQ U ES T [ ' url ' ]) ,
" <br > are </ div > $ ex trac t ed_ n ame s \ n";
} else {
ech o <<< FOR M
< form m et hod =" post " >
<label > Enter a URL :
< in pu t type =" text " nam e = " url " size = " 100 "
value = " htt p :// cgi . csc . liv . ac . uk /
ul lr ich / COMP 28 4 / t es ts / a1 te st1 . txt " >
</label ><br > < br >
< in pu t type =" submit " val ue = " Extr ac t N am es " >
</form >
FOR M ;
}
?>
</body > </ html >
http://cgi.csc.liv.ac.uk/
~
ullrich/COMP519/examples/extract_names.php
COMP519 Web Programming Lecture 23 Slide L23 11
Available Information and Input Form Data
Forms in PHP: Example (1)
extraction.php
<? php
funct i on e xtra ct_n ames ( $url ) {
$text = f ile _ge t_c o nte nts ( $url );
if ( $ text === fa l se )
return " ERROR : IN VALID URL !" ;
else {
$corr ect = pre g _ma tch_ all ( " / name ={( [^\} ]+)} / " ,
$text , $ matches , PR EG_ PAT TER N _O R DER );
if ( $ correc t == 0) return " ERROR : NO NAMES FOUND " ;
$count = arr ay_ cou nt_ val ues ( $matc hes [1]);
arsort ( $ cou n t );
foreach ( $ cou nt as $ n a me => $ n umber ) {
$table .= " <tr >< td > $name </ td > <td > $ number </ td > </ tr >" ;
}
$table = " < table > < thead > <tr > <th > Name </ th >< th > No of occur " .
" rences </th > </ tr > </ thead >< tbody >" . $ tab le . " </ tbody > </ table >" ;
return $ ta ble ;
} }
?>
http://cgi.csc.liv.ac.uk/
~
ullrich/COMP519/examples/extraction.php
COMP519 Web Programming Lecture 23 Slide L23 12
Further Reading
Revision and Further Reading
Read
Chapter 11: Form Handling
of R. Nixon: Learning PHP, MySQL & JavaScript:
with jQuery, CSS & HTML5. O’Reilly, 2018.
COMP519 Web Programming Lecture 23 Slide L23 13