Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
Open Source & PHP



Ajax and PHP

A three hour tutorial I gave at PHP Quebec on the challenges, theory, and concepts behind making asynchronous JavaScript calls for Web 2.0 Applications using PHP

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Ajax and PHP

  1. 1. Ajax and PHP John Coggeshall
  2. 2. Welcome! <ul><li>Who am I: John Coggeshall </li></ul><ul><ul><li>Sr. Technical Consultant, Zend Technologies </li></ul></ul><ul><ul><li>Author PHP 5 Unleashed </li></ul></ul><ul><ul><li>Zend Educational Advisory Board </li></ul></ul><ul><ul><li>Speaker on PHP-related topics worldwide </li></ul></ul><ul><ul><li>Geek </li></ul></ul>
  3. 3. Why are we here? <ul><li>We’re here to discuss AJAX </li></ul><ul><ul><li>… and PHP </li></ul></ul><ul><ul><li>… and XML </li></ul></ul><ul><ul><li>… and Javascript </li></ul></ul><ul><ul><li>… and Networks </li></ul></ul><ul><li>In this three hour tutorial, I’ll be explaining a number of AJAX-related concepts </li></ul>
  4. 4. Fair Warning <ul><li>I’ll warn you right now – I work for Zend, not Netscape </li></ul><ul><ul><li>I am not a client-side developer </li></ul></ul><ul><ul><li>I do not know which browsers support which constructs of Javascript under which conditions using which technologies on which operating system </li></ul></ul><ul><ul><li>I am a PHP developer responsible for scaling numerous mission-critical PHP sites and technologies </li></ul></ul><ul><ul><li>I do understand Internet architectures and how to scale them in practical environments </li></ul></ul><ul><ul><li>I do understand enough about AJAX as a technology to speak intelligently </li></ul></ul><ul><ul><li>Don’t expect a lot of flashy AJAX demos here </li></ul></ul>
  5. 5. The basics <ul><li>So, what does AJAX stand for anyway? </li></ul><ul><ul><li>A synchronous J avascript a nd X ML </li></ul></ul><ul><li>The basic idea: </li></ul><ul><ul><li>Javascript is the reigning champion of the client side </li></ul></ul><ul><ul><ul><li>Image roll-overs </li></ul></ul></ul><ul><ul><ul><li>DHTML </li></ul></ul></ul><ul><ul><ul><li>Client-side form processing </li></ul></ul></ul><ul><ul><li>Not all information and processes can be given to the client </li></ul></ul><ul><ul><ul><li>Insecure / Untrusted </li></ul></ul></ul><ul><ul><ul><li>Simple processing ability restrictions </li></ul></ul></ul>
  6. 6. Asynchronous Javascript <ul><li>AJAX allows us to take advantage of the server for information, while leaving the GUI-related items to the client </li></ul><ul><li>It’s not a new technology </li></ul><ul><ul><li>Just has a neat acronym now </li></ul></ul><ul><li>How’s it work? </li></ul><ul><ul><li>Javascript applications perform requests to the server using an agreed protocol </li></ul></ul><ul><ul><li>The server responds in kind with the requested information </li></ul></ul><ul><ul><li>All of this takes place without reloading the page </li></ul></ul><ul><ul><ul><li>Asynchronous of the client </li></ul></ul></ul><ul><ul><li>Javascript then processes the result and manipulates the page </li></ul></ul>
  7. 7. Don’t confuse technologies <ul><li>Is AJAX Gmail / Google Maps? </li></ul><ul><ul><li>No </li></ul></ul><ul><li>Is AJAX Prototype or </li></ul><ul><ul><li>No </li></ul></ul><ul><li>Is AJAX Ruby-on-Rails? </li></ul><ul><ul><li>No </li></ul></ul><ul><li>AJAX is simply the idea of enabling your browser to communicate asynchronously with the server to provide a more rich user “Web 2.0” experience. </li></ul>
  8. 8. Implementing AJAX <ul><li>Step 1: Open a asynchronous connection from the client to the server </li></ul><ul><li>Step 2: Perform a request against the server using an agreed upon protocol </li></ul><ul><li>Step 3: Process the results via Javascript and manipulate the client without causing a full refresh of the page </li></ul><SCRIPT language=&quot;JavaScript&quot;> <!-- pic1= new Image(100,25); pic1.src=&quot;;; //--> </SCRIPT>
  9. 9. “ Traditional” AJAX <ul><li>Despite the misconceptions on what exactly AJAX is, it does have a traditional approach </li></ul><ul><ul><li>XMLHttpRequest object </li></ul></ul><ul><ul><ul><li>Available in most modern browsers </li></ul></ul></ul><ul><ul><ul><li>Identical in concept to the Image object </li></ul></ul></ul><ul><ul><li>Allows you to retrieve data from the server without performing an entirely new request </li></ul></ul><ul><li>Requests are generally made in conjunction with a particular Javascript event </li></ul><ul><ul><li>i.e. onBlur of a zip-code field which automatically finds out the city / state </li></ul></ul>
  10. 10. “ Traditional” AJAX <ul><li>Okay, so here we go: </li></ul><ul><ul><li><input type=“text” size=“5” onBlur=“updateCityState()”> </li></ul></ul><ul><li>Now all we need to do is implement a javascript updateCityState() function that creates an XMLHttpRequest object </li></ul><ul><li>Then we take that object and request a PHP page </li></ul><ul><li>… parse the result </li></ul><ul><li>… update the city and state input fields to reflect the new information! </li></ul>
  11. 11. Browser Wars Revisited <ul><li>Ah, if only it were that simple </li></ul><ul><ul><li>Unfortunately, XMLHttpRequest is implemented in different ways on each browser </li></ul></ul><ul><ul><li>Requires lots of Javascript black-magic that I don’t know to ensure you’re creating the proper object the proper way </li></ul></ul><ul><li>My solution: Google </li></ul><ul><ul><li>This problem has been solved a million times over so I won’t re-explain the wheel here </li></ul></ul>
  12. 12. Establishing a Protocol <ul><li>Now that you’ve made a request back to the web server (in this case, using PHP and HTTP GET) time to deal with the response </li></ul><ul><ul><li>This is where things really go amuck </li></ul></ul><ul><li>There is no standard AJAX protocol, the data can be anything </li></ul><ul><ul><li>Comma separated fields </li></ul></ul><ul><ul><li>Serialized Javascript </li></ul></ul><ul><ul><li>Custom XML </li></ul></ul><ul><ul><li>SOAP </li></ul></ul><ul><ul><li>URLEncoded fields </li></ul></ul><ul><ul><li>20 bytes of data, each byte representing a command </li></ul></ul>
  13. 13. Establishing a Protocol <ul><li>While there are no standards per-se, there are common techniques </li></ul><ul><ul><li>Future versions of PHP will support JSON encoding by default </li></ul></ul><ul><ul><li>Allows you to pass complex data types back and forth between PHP and Javascript fairly easily </li></ul></ul><ul><ul><li>You can download JSON encoding support from PECL </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><ul><ul><li>$json_enc = json_encode(array(1,2,3)); </li></ul></ul></ul><ul><ul><ul><li>$json_dec_var = json_decode(‘{ “abc”:12 }’); </li></ul></ul></ul><ul><ul><ul><li>javascript:eval(‘{ “abc”:12}’); // return in JS </li></ul></ul></ul>
  14. 14. AJAX without XmlHttpRequest <ul><li>Now that you have the basic jist, the cleaver among you must realize that XmlHttpRequest isn’t necessary </li></ul><ul><li>With some crafty HTML you can do your AJAX request using “standard” browser facilities </li></ul><ul><ul><li>Step 1: Use Javascript to create a new <SCRIPT> tag in the document </li></ul></ul><ul><ul><li>Set the source of this script tag dynamically to our PHP backend URL and provide the “output element” ID we are interested in manipulating </li></ul></ul><ul><ul><li>Have our backend written in PHP process the request and return Javascript manipulating that ID as we saw fit. </li></ul></ul><ul><li> </li></ul>
  15. 15. I said it was a synchronous <ul><li>Regardless of the approach you use to generate an AJAX request, always remember that it is an ASYNCHRNONOUS request. </li></ul><ul><ul><li>Performing a behind the scenes synchronous request stands a very good chance of locking up IE </li></ul></ul><ul><ul><li>Every second the server takes the respond to the client in a synchronous request is a second the browser is not responding to input </li></ul></ul><ul><ul><li>Bad.. Bad… BAD </li></ul></ul>
  16. 16. HTTP GET vs. POST <ul><li>This one personally really urkes me about web developers </li></ul><ul><ul><li>GET is for GETTING data </li></ul></ul><ul><ul><li>POST is for POSTING data </li></ul></ul><ul><li>Sending a GET request should never cause an update on the server </li></ul><ul><ul><li>Reason 1: GET requests should be bookmark-able </li></ul></ul><ul><ul><li>Reason 2: GET requests should be cache-able </li></ul></ul><ul><li>If you use AJAX for anything other then retrieving data then use HTTP POST for those actions </li></ul>
  17. 17. Why I am scared of AJAX
  18. 18. One of the biggest problems with AJAX <ul><li>Let’s imagine that each request sent over the wire is like a car driving from point A (the client) to point B (the server) </li></ul><ul><li>Roads are Networks </li></ul>
  19. 19. One of the biggest problems with AJAX
  20. 20. One of the biggest problems with AJAX <ul><li>Simple requests seem to work just fine… </li></ul>
  21. 21. One of the biggest problems with AJAX
  22. 22. One of the biggest problems with AJAX
  23. 23. One of the biggest problems with AJAX
  24. 24. One of the biggest problems with AJAX <ul><li>The problem with AJAX has to do with multiple dependent asynchronous requests </li></ul><ul><ul><li>You can’t rely on any order of operations in classical AJAX models </li></ul></ul>
  25. 25. One of the biggest problems with AJAX
  26. 26. One of the biggest problems with AJAX
  27. 27. One of the biggest problems with AJAX
  28. 28. One of the biggest problems with AJAX
  29. 29. Some requests will happen faster <ul><li>When working with AJAX, always know you cannot rely on one request finishing before the next is triggered </li></ul><ul><li>Requests can take different lengths of time based on a huge array of factors </li></ul><ul><ul><li>Server load and Network load come to mind </li></ul></ul><ul><li>Can really mess up your application </li></ul><ul><li>Bad news: None of the current AJAX toolkits account for this latency </li></ul>
  30. 30. Developing with Latency in mind <ul><li>A number of tools exist for developing AJAX applications with latency in mind </li></ul><ul><ul><li>AJAX Proxy is a good example </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><ul><ul><li>Allows you to simulate latency in your requests </li></ul></ul></ul><ul><ul><li>You can use it in conjunction with “SwitchProxy” to point your browser at a different proxy server to use it </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><li>Not a true solution, but at least let’s you test for the problem. </li></ul>
  31. 31. AJAX: Redefining the notion of state? <ul><li>Now that we are talking about AJAX intelligently, let’s talk about a very important aspect to the modern web application: sessions </li></ul><ul><ul><li>Sessions allow current web applications to maintain state across stateless HTTP requests </li></ul></ul>
  32. 32. Throw cookies away? <ul><li>In AJAX models, these session cookies are no longer necessary </li></ul><ul><ul><li>In-memory data received from the server during an AJAX request is state </li></ul></ul><ul><ul><li>Lends itself much more to the classical MVC / Messaging model of client-side applications </li></ul></ul><ul><ul><li>As long as the user doesn’t “close” the application…. </li></ul></ul><ul><ul><ul><li>Clicking reload </li></ul></ul></ul><ul><ul><ul><li>Closing the window </li></ul></ul></ul><ul><ul><li>…. Then they’re state is being tracked </li></ul></ul>
  33. 33. Requests per second (Traditional) <ul><li>Other then actually working, scaling a web application is the most important architectural consideration </li></ul><ul><ul><li>(accurate) Requests per second is key metric </li></ul></ul><ul><li>Consider what happens during a single server/single client exchange </li></ul>
  34. 34. Requests per second (Traditional) <ul><li>Servers are limited to a maximum requests per second by numerous factors </li></ul><ul><li>To scale: </li></ul><ul><ul><li>Make the maximum sustainable RPS number as high as possible </li></ul></ul><ul><ul><ul><li>Faster script execution times </li></ul></ul></ul><ul><ul><ul><li>Faster database access </li></ul></ul></ul><ul><ul><li>Make the most of every request </li></ul></ul><ul><ul><ul><li>Avoid costly unnecessary handshakes </li></ul></ul></ul><ul><ul><ul><li>Intelligently segment content </li></ul></ul></ul>
  35. 35. Requests per second (Traditional) <ul><li>Common scaling trick: static content farms </li></ul><ul><ul><li>Off-load non-logic-based content serving to lightweight and fast HTTP servers </li></ul></ul>
  36. 36. Requests per second (AJAX) <ul><li>Looking at the AJAX philosophy it’s clear a different request pattern exists </li></ul><ul><ul><li>Relatively heavy and common load spikes </li></ul></ul><ul><ul><li>Very frequent and relatively quick follow-up requests </li></ul></ul><ul><li>While some tricks can be borrowed from the old models, clearly a new pattern of scaling must be introduced </li></ul>
  37. 37. Optimizing AJAX pages <ul><li>Single-serve client libraries </li></ul><ul><ul><li>Use tools to combine multiple JavaScript/CSS files into a single giant file to reduce the load on the server to a single request to load the application logic </li></ul></ul><ul><ul><ul><li>Can be cached on the client </li></ul></ul></ul><ul><li>Avoid first-execution spikes </li></ul><ul><ul><li>Design your applications upon initial execution to perform a single AJAX request to effectively populate the entire page </li></ul></ul><ul><ul><ul><li>Reduces strain on both the pipeline and on your backend database servers </li></ul></ul></ul>
  38. 38. A thought
  39. 39. The Future of AJAX?
  40. 40. Thank you! Questions?
  • SadiaSaeed5

    Nov. 17, 2020
  • shanmugarajan9

    Aug. 30, 2017
  • SohailAhmad29

    Dec. 22, 2015
  • imamsuwito

    May. 28, 2015
  • RajeshReddy31

    Sep. 21, 2013
  • googleshokry

    Nov. 20, 2012
  • EdyPurwanto1

    Jun. 30, 2011
  • bookhouseboy

    Apr. 12, 2011
  • sridhar481

    Nov. 15, 2010
  • lightningfullmoon

    Aug. 19, 2009
  • pec1985

    Jul. 23, 2009
  • brainsqueezer

    Jul. 8, 2009
  • krunople

    May. 2, 2009
  • danimatos

    Apr. 14, 2009
  • dugumao

    Mar. 31, 2009
  • shureg

    Mar. 28, 2009
  • menaceh2k

    Aug. 25, 2008
  • mokin

    Aug. 17, 2008
  • charliez

    May. 12, 2008
  • supercoco9

    Apr. 23, 2008

A three hour tutorial I gave at PHP Quebec on the challenges, theory, and concepts behind making asynchronous JavaScript calls for Web 2.0 Applications using PHP


Total views


On Slideshare


From embeds


Number of embeds