Home > Articles

PHP and the Web

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Server-side Versus Client-side

One of the keys to understanding PHP's role in the Web is to understand how the Web works at a fundamental level. This generally involves a basic understanding of HTTP, Hypertext Transfer Protocol. To examine the basic operation of the Web, consider a typical HTTP client, your Web browser. When you visit a URL such as http://example.org/, your browser sends an HTTP request to the web server at example.org. The simplest example of this request is as follows:

GET / HTTP/1.1
Host: example.org

The web server's responsibility is to respond to this request, preferably with the resource that is desired (the document root in this example). An example of a response is as follows:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 419

<html>
<head><title>Example Web Page</title></head>
<body>
<p>You have reached this web page by typing &quot;example.com&quot;, 
&quot;example.net&quot;, or &quot;example.org&quot; into your web browser.</p>
<p>These domain names are reserved for use in documentation and are not 
available for registration. See 
<a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC 2606</a>, Section 
3.</p>
</body>
</html>

As you should notice, the majority of this response is the actual content, the HTML. When your browser receives this response, it will render the web page (see Figure 3.1). Once a page is rendered, you can disconnect your computer from the Internet, and this won't cause a problem until your browser needs to send another HTTP request.

Where does PHP fit into this process? PHP's role is best explained as an aid to the web server while it is generating the HTTP response. Thus, by the time the web server sends the response, PHP's job is done. Its output is included in the response. Because PHP's activity takes place on the server, it is an example of a server-side technology.

By contrast, any processing that takes place after the browser has received the response is referred to as client-side. JavaScript is a popular choice for client-side scripting. You're probably familiar with using JavaScript or at least seeing it when you view the source of a web page. This is a distinguishing characteristic. What you see when you view the source of a web page is the content of the HTTP request. This content can be generated on the server, so just as PHP can be used to generate HTML, it can also generate JavaScript.

Figure 3.1Figure 3.1 A browser renders a web page.

JavaScript executes on the client. Thus, interacting with PHP is much more difficult because it requires another HTTP request to be sent. After all, PHP's job is done, and the web server is quietly awaiting the next request. By the time JavaScript executes, there isn't even a connection between the Web client (your browser) and the web server anymore.

If you find yourself having trouble determining whether you can pass data from PHP to JavaScript or from JavaScript to PHP, it would be wise to review this section a few times. A clear understanding of the environment in which PHP operates, and the distinction between client-side and server-side technologies, is important.

  • + Share This
  • 🔖 Save To Your Account