Hardware Reference
In-Depth Information
Of course, because PHP is a
programming language, you can do
more than just print out the results.
Try the script to the right.
8
<?php
/*
Age checker
Context: PHP
Try requesting this script with the same
parameter string as the last script,
?name=tom&age=14 , and see what
happens. Then change the age to a
number greater than 21.
Expects two parameters from the HTTP request:
name (a text string)
age (an integer)
Prints a personalized greeting based on the name and age.
*/
NOTE: One great thing about PHP is that
it automatically converts ASCII strings of
numbers like “14” to their numerical values.
Because all HTTP requests are ASCII-based,
PHP is optimized for ASCII-based exchanges
like this.
// read all the parameters and assign them to local variables:
foreach ($_REQUEST as $key => $value)
{
if ($key == "name") {
$name = $value;
}
if ($key == "age") {
$age = $value;
}
}
if ($age < 21) {
echo "<p> $name, You're not old enough to drink.</p>\n";
} else {
echo "<p> Hi $name. You're old enough to have a drink, ";
echo "but do so responsibly.</p>\n";
}
?>
POST is the method usually used to post data from a web
form. Instead of adding the parameters on the end of the
URL path as GET does, POST adds the parameters to the
end of the whole HTTP request. There are a couple other
parameters you need to add to a POST request too, like
the content type and the content length. POST is a little
more work to set up, but it's really useful for hiding the
messy business of passing parameters, keeping your URLs
tidy and easy to remember. Instead of the previous URL,
all the user has to see is:
HTTP GET and POST
The ability to respond to parameters sent in an HTTP
request opens all kinds of possibilities. For example,
you can write a script that lets you choose the address
to which you send an email message, or what message
to send. You'd just add parameters to the URL after the
question mark, read them in PHP, and use them to set the
various mail variables.
The method for sending variables in the URL after a
question mark is called GET, and it's one of HTTP's four
commands. Besides GET, you can also POST, PUT, and
DELETE. Many browsers don't support PUT or DELETE,
however, so stick to GET and POST for now.
http:// www.example.com /get-parameters.php
The rest can get delivered via POST.
 
Search WWH ::




Custom Search