WAP-Enabling a Website with PHP3 - How it Works (
Page 2 of 4 )
The CGI program runs on response to every incoming query and is easily fast
enough to fill our data pipe without having to resort to trickery like Apache
modules or fastCGI. Each execution of the CGI inspects the incoming form and
uses that to decide which presentation template should be used. These
presentation templates (nothing to do with the C++ `template' construct) are
php3 pages. The CGI program's output is essentially a long list of php3 data
definitions; here's a fragment:
<?
$logfilename="/www/web.places/logs/dblog";
header("Set-cookie: name=3880d6350463;
expires=Saturday, 23-Mar-2002 06:08:30 GMT; path=/");
$Mytemplate["formmethod"]="get";
$Mytemplate["sessionkey"]="3880d6350463";
$Mytemplate["retrieved_cookie_session"]="Fri Apr 21 20:11:05 2000
";
$Mytemplate["lasttimeon"]="956344265";
$Mytemplate["placetype"]="1";
$Mytemplate["QUERY"]="a=show_link&unique_id=175&placetype=1";
$Mytemplate["mapimagewidth"]="292";
$Mytemplate["mapimageheight"]="219";
$Mytemplate["east"]="414996";
$Mytemplate["north"]="435499";
$Mytemplate["placename"]="Park";
$Mytemplate["title"]="Place Details, Park";
$Mytemplate["OSGB"]="SE 149 354";
$Mytemplate["latlong"]="053 48\" 55'N 001 46\" 19'W";
... and a
lot more like it.
After spitting out the data, the CGI program opens the php3 template file and
tacks it onto the end of the data definitions, so it's acting much like the
`cat' command. The whole pasted-together chunk is sent through a pipe into the
php3 interpreter and the output of that is also piped asynchronously
through `weblint'; if weblint picks up any errors, it emails us to warn that we
are generating bad HTML. All this is done for every page generated. Running on
an AMD K6/250 it looks as if we can do about one page per second, including the
map generation before we would have to turn off the weblinting or look for more
power.
Here is a fragment from the `place' php3 template, this is the one that
executes if you click on the link to The
Park pub:
<?
require('stdhead.tpl');
require('stdplace.tpl');
$thisplace = new PlaceDetail();
if (isset($place)) { $thisplace->import($place[0]); }
$thistypes = new TypeList();
if (isset($type)) { $thistypes->import($type); }
$REenter = "sessionkey=" . R("sessionkey") . "&rlimit=" .
R("rlimit") . "&east=" . R("east") . "&north=" . R("north")
. "&unique_id=" . $thisplace->unique_id .
"&placetype=" . R("placetype");
if(I("o-east")){
$REenter .= "&o-east=" . R("o-east") . "&o-north=" . R("o-north");
}
$basicformfields = "<input type=hidden name=a value=\"show\">\n";
$basicformfields .= "<input type=hidden name=placetype value=\"" .
R("placetype") . "\">\n";
$basicformfields .= "<input type=hidden name=sessionkey value=\"$K\">\n";
$basicformfields .= "<input type=hidden name=rlimit value=\"" .
$Mytemplate["rlimit"]. "\">\n";
if (I("o-east")){
$basicformfields .= "<input type=hidden name=o-east value=\"" .
$Mytemplate["o-east"]. "\">\n";
$basicformfields .= "<input type=hidden name=o-north value=\"" .
$Mytemplate["o-north"] . "\">\n";
}
$basicformfields .= "<input type=hidden name=east value=\"" .
$Mytemplate["east"]. "\">\n";
$basicformfields .= "<input type=hidden name=north value=\"" .
$Mytemplate["north"]. "\">\n";
$basicformfields .= "<input type=hidden name=\"unique_id\" value=\"" .s
$thisplace->unique_id . "\">\n";
$votepics = array("twocross.gif", "onecross.gif", "onetick.gif",
"twotick.gif", "threetick.gif");
$votealts = array(":<", ":(", ":|", ":)", ":>");
// and a lot more besides ...
It's probably obscure unless you know php3 but I thought I'd
include it for those who know it. A lot of the calls like
R(),
I() and so on are functions defined in
stdhead.tpl - they
extract data from the predefined arrays but also report errors if expected data
is missing, helping us to debug the interface between the CGI and the php.
That all works - the CGI->php3->weblint->user chain is highly
effective. By having the logic in the C++ and the HTML generation in the php3
two people or teams can work simultaneously on the project without getting in
each other's way much and what's more, if you are tinkering with layout you
know you can't break the data engine or corrupt the database easily. This
is typically not the case with `traditional' php3 work where data
management and layout get badly intermingled. At least they do when I'm writing
the stuff, maybe there are some star performers who can manage to do better; I
take my hat off to them if they exist.
This article copyright GBdirect Ltd 2000. All rights reserved.
Reprinted from http://www.gbdirect.co.uk/ with
permission.