11.07.2015 Views

AJAX and PHP

AJAX and PHP

AJAX and PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!