
A MORE COMPLEX NETWORK 107
<?php
/*
Cat On Mat
Language: PHP
Expects a parameter called SensorValue, an integer.
Prints a custom message depending on the value of SensorValue.
*/
$threshold = 320; // minimum sensor value to trigger action
// print the beginning of the HTML page:
echo "<html><head></head><body>\n";
// read all the parameters and assign them to local variables:
foreach ($_REQUEST as $key => $value)
{
if ($key == "sensorValue") {
$sensorValue = $value;
}
}
// respond depending on the sensor value:
if ($sensorValue > $threshold) {
echo "<p> The cat is on the mat.</p>\n";
} else {
echo "<p> the cat is not on the mat.</p>\n";
}
// finish the HTML:
echo "</body></html>\n";
?>
8 In order for the PHP scripts to run, you’ll need
to install them on a web server that supports PHP.
There are many web hosting companies with
inexpensive (less than $10 a month) web hosting
plans that support PHP.
Save it to your server with the name cat-script.
php. Test it from a browser with this HTTP
request, using different values for sensorValue. Use a URL
similar to the one shown here, replacing www.example.
com with your server name, and adjusting the path to the
script as needed: http://www.example.com/catcam/cat-
script.php?sensorValue=12. Any value above 320 should
result in a message that the cat is on the mat. When you’re
satisfied that it works, change your ...