Web Programming
Although you will find it used in scenarios as diverse as quality control and point-of-
sale systems, PHP was designed primarily as a Web-development language, and that
remains its most common use to this day.
In this chapter, we focus on the features of PHP that make it such a great choice
for developing Web applications, as well as some Web-related topics that you should
be familiar with in order to take the exam.
Anatomy of a Web Page
Most people think of a Web page as nothing more than a collection of HTML code.
This is fine if you happen to be a Web designer—but, as a PHP developer, your knowl-
edge must run much deeper if you want to take full advantage of what the Web has
to offer.
From the point of view of the Web server, the generation of a document starts with
an HTTP request, in which the client requests access to a resource using one of a
short list of methods. The client can also send a data payload (called request) along
with its request—for example, if you are posting an HTTP form, the payload could
consist of the form data, while if you are uploading a file, the payload would consist
of the file itself.
Once a request is received, the server decodes the data that it has received and
passes it on to the PHP interpreter (clearly, we are assuming that the request was
98 ” Web Programming
made for a PHP script—otherwise, the server can choose a different handler or, in
the case of static resources, such as images, output them directly).
Upon output, the server first writes a set of response headers to the clients; these
can contain information useful to the client—such as the type of content being re-
turned, or its encoding, as well as data needed to maintain the client and the server
in a stateful exchange (we’ll explain this later).
Forms and URLs
Most often, your script will interact with their clients using one of two HTTP meth-
ods: GET and POST. From a technical perspective, the main difference between these
two methods is in the fact that the latter allows the client to send along a data pay-
load, while the former only allows you to send data as part of the query string.
This, of course, doesn’t mean that you can’t submit a form using GET—only that
you will be somewhat limited in the size and type of data that you can send. For
example, you can only upload files using POST, and almost all browsers implement
limitations on the length of the query string that confine the amount of data you can
send out with a GET operation.
i Contrary to popular belief, POST is not an inherently more secure to submit forms
than GET. We explain this concept in greater detail in the Security chapter.
From an HTML perspective, the difference between GET and POST is limited to the
action attribute of the <form> element:
<!--Form submitted with GET-->
<form action="index.php" method="GET">
List: <input type="text" name="list" /><br />
Order by:
<select name="orderby">
<option value="name">Name</option>
<option value="city">City</option>
<option value="zip">ZIP Code</option>
</select><br />
Sort order:
Web Programming ” 99
<select name="direction">
<option value="asc">Ascending</option>
<option value="desc">Descending</option>
</select>
</form>
<!--Form submitted with POST-->
<form action="index.php" method="POST">
<input type="hidden" name="login" value="1" />
<input type="text" name="user" />
<input type="password" name="pass’ />
</form>
GET and URLs
When a form is submitted using the GET method, its values are encoded directly in
the query string portion of the URL. For example, if you submit the form above by
entering user in the List box and choosing to sort by Name in Ascending order, the
browser will call up our index.php script with the following URL:
http://example.org/index.php?list=user&orderby=name&direction=asc
As you can see, the data has been encoded and appended to the and of the URL for
our script. In order to access the data, we must now use the $_GET superglobal array.
Each argument is accessible through an array key of the same name:
echo $_GET[’list’];
You can create arrays by using array notation...
http://example.org/index.php?list=user&order[by]=column&order[dir]=asc
..and then access them using the following syntax:
echo $_GET[’order’][’by’];
echo $_GET[’order’][’direction’];
100 ” Web Programming
Note that, clearly, there is nothing that stops you from creating URLs that already
contain query data—there is no special trick to it, other than the data must be en-
coded using a particular mechanism that, in PHP, is provided by the urlencode()
function:
$data = "Max & Ruby";
echo "http://www.phparch.com/index.php?name=" . urlencode ($data);
The PHP interpreter will automatically decode all incoming data for us, so there is
no need to execute urldecode() on anything extracted from $_GET.