AJAX and PHP
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Server-Side Techniques with <strong>PHP</strong> <strong>and</strong> MySQL<br />
function error_h<strong>and</strong>ler($errNo, $errStr, $errFile, $errLine)<br />
{<br />
// clear any output that has already been generated<br />
if(ob_get_length()) ob_clean();<br />
// output the error message<br />
$error_message = 'ERRNO: ' . $errNo . chr(10) .<br />
'TEXT: ' . $errStr . chr(10) .<br />
'LOCATION: ' . $errFile .<br />
', line ' . $errLine;<br />
echo $error_message;<br />
// prevent processing any more <strong>PHP</strong> scripts<br />
exit;<br />
}<br />
?><br />
6. Load http://localhost/ajax/foundations/smartproxyping/<br />
smartproxyping.html. The output should look like the one in Figure 3.15.<br />
What Just Happened?<br />
Our client, in this example, knows how to check from time to time if the server is available. The<br />
r<strong>and</strong>om number generator service provides the page http://www.r<strong>and</strong>om.org/cgi-bin/checkbuf<br />
—which you can use to check its buffer level.<br />
The JavaScript code in smartproxyping.js starts by defining a number of global variables that<br />
you use to control the program's behavior:<br />
// holds the remote server address <strong>and</strong> parameters<br />
var serverAddress = "smartproxyping.php";<br />
var getNumberParams = "action=GetNumber" + // get a new r<strong>and</strong>om number<br />
"&min=1" + // the min number to generate<br />
"&max=100"; // the max number to generate<br />
var checkAvailabilityParams = "action=CheckAvailability";<br />
// variables used to check for server availability<br />
var requestsCounter = 0; // counts how many numbers have been retrieved<br />
var checkInterval = 10; // counts interval for checking server availability<br />
var updateInterval = 1; // how many seconds to wait to get a new number<br />
var updateIntervalIfServerBusy = 10; // seconds to wait when server busy<br />
var minServerBufferLevel = 50; // what buffer level is considered acceptable<br />
These variables contain the data required to make server requests. getNumberParams contains the<br />
query string parameters needed to request a new r<strong>and</strong>om number, <strong>and</strong> checkAvailabilityParams<br />
contains the parameters used to check the server's buffer level. The other variables are used to<br />
control the intervals for making the asynchronous requests.<br />
A novelty in this exercise compared to the previous ones is that you have two functions that<br />
h<strong>and</strong>le server responses—h<strong>and</strong>leCheckingAvailability <strong>and</strong> h<strong>and</strong>leGettingNumber. The roots of<br />
this happen to be in the process() function, which assigns one of these callback functions<br />
depending on the server action it requests.<br />
In this program, process() is not called only once as in other exercises; instead, it is called<br />
multiple times, <strong>and</strong> each time it must decide what action to make—should it ask for a new r<strong>and</strong>om<br />
number, or should it check the server's buffer level? The requestsCounter variable, which keeps<br />
a track of how many times we have retrieved a new r<strong>and</strong>om number since the last buffer check,<br />
helps us make a decision:<br />
98<br />
www.it-ebooks.info