Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

Communication between client and server

  1. What is PHP and what does it?
  2. A request of a file seen by a client
  3. Request of a file in the view of the server

1. What is PHP and what does it?

PHP is the recursive backronym of PHP: Hypertext Preprocessor. The name indicates something is proceeded before some point. In this case all the php statements inside the file are proceeded before the server sends the file (or be more specific: the output of the php script) to the client. This is similar to the well known C preprocessor.

PHP is a module or programm installed on the webserver. Often other usefull programms like MySQL is installed. The following diagramm illustrate how it looks like.

Installed modules on the webserver Img.:Installed modules on the webserver

As shown in the diagramm php isn't related to the client or its browser in any way. This is the reason why you cannot control the client. Actions like starting Winamp are not possible.

Common usage of PHP is the processing of data given from a HTML form, that has been send to the webserver. As the php scripts running on the webserver they can access and work with any databases installed on the server (like MySQL as mentioned above). So you can easily write news scripts which reads the news stored in a database. The result of each php script is still plain html code. The browser doesn't recognized in any way the html code isn't generated by a php script. Of course the user see the extensions .php in the URL. However, neither the browser nor the client using it can access the php code you wrote inside the php file.

2. A request of a file seen by a client

If the client requests a file from a server the following happends.

Client send request to the server Img.:Client send request to the server

In the viewpoint of the client he gets the answer of the server.

Client receives the response from the server Img.:Client receives the response from the server

The client used only one connection to the server, however he get two seperated contents from the server.

  1. First the client receives the so called headers. These headers describe the following content and contains several informations about the server. These data are normally hidden by the user but can be shown with browser extensions like Firebug. Headers looks like this:

    HTTP/1.1 200 OK
    Date: Mon, 26 Nov 2007 22:37:21 GMT
    Server: Apache
    Content-Type: text/html

    It's possible to change all these headers with php before they are send to the client. This also means it is possible to generate and send image data to the client if you set the proper content type like Content-Type: image/png.

  2. After the headers there are the so called content. In most cases it containts the html code generated by the php script but can also be the binary data of an image.

You can change both parts with php as much you want, but there is one restriction. The headers must be send before you send the content. These happends automatically if you send the first part of the content. If you print some stuff with php you cannot change the headers. If you try this anyway you will get an error message like Cannot modify header information - headers already sent by (output started at ...).

3. Request of a file in the view of the server

If the server receives a request to a html file he just read the file from the hard disk and send it to the client.

Server receives the request from the client Img.:Server receives the request from the client

As the server must send headers to the client he generate the headers from the files content. E.g. he check the file size and generate a Content-Length header. A complete list of headers may be look like this.

HTTP/1.1 200 OK
Date: Wed, 28 Nov 2007 16:34:05 GMT
Server: Apache
Last-Modified: Mon, 26 Nov 2007 23:24:33 GMT
ETag: "d9b8a-2301-43fdd444c0e40"
Accept-Ranges: bytes
Content-Length: 8961
Content-Type: text/html

If the headers are send the server sends the html code of the file and is finished with this request.

Server sends the response to the client Img.:Server sends the response to the client

As the server hasn't much to do this request is handled very quickly. However, if the file is a php script the server has much more to do.

Server receives the request from the client Img.:Server receives the request from the client

Now the server reads the content of the file. This is a similar to a normal html file.

Server reads the file contents Img.:Server reads the file contents

Now the processing of php must be followed. As the server itself don't understand php he uses the php interpreter. This interpreter reads and executes the statements inside the file accordingly to the rules of the programming language.

Server processed the php statements with the php interpreter Img.:Server processed the php statements with the php interpreter

If the php interpreter finishes the webserver again get the two parts headers and content. These are send to the client back.

Server sends the response to the client Img.:Server sends the response to the client

In point of view of the client nothing is changed. He doesn't even notice php is installed on the server and the requested file contained php code. But the user notice a performance lost if he access php files from a webserver. As php files must be parsed and executed by an php interpreter the server needs much more time (compared to a single html file) to send a response to the client.

Questions about the chapter

1. What are the two parts sends back to the client?

During a http connection the two parts headers and content are send back to the client, and in exactly this order. For a detailed overview of the transfer between client and server check the RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1.

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!