How PHP Works with Web Server
In this tutorial, we will understand how PHP works with the web server through an example. You know that HTML (Hypertext Markup Language) is a language used to create the web pages of an application. There are two types of web pages:
- static webpages
- dynamic webpages
A static web page is an HTML document that does not change until the web programmer manually changes it. This is a plain text file with an extension of .html that contains all the content to be displayed in the web browser.
One of the main problems with static web pages is that they do not change in response to user input if they are stored on the web server.
Let’s first understand how a web server processes a static web page step by step. Then, we will understand how PHP works with a server to create a dynamic web page.
How Web Server Processes Static Web Pages?
There are the following steps through which a web server processes a request for a static web page. They are as follows:
1. How Browser Uses DNS to Connect to Websites:
When the user enters an URL http://scientecheasy.com into his web browser’s address bar, the browser looks up the IP address for scientecheasy.com. Remember that every machine attached to the Internet has an IP address, including your computer. Generally, we access web servers by name, such as scientecheasy.com.
As you probably know, when a browser needs to connect to a website, it consults an additional internet service called the Domain Name Service (DNS) to find its associated IP address. Once it found the IP address, the browser uses it to communicate with the server hosting the website.
2. How Browser Sends HTTP Requests to Servers
When a user requests for the home page at scientecheasy.com in a web browser, the request crosses the Internet and reaches at the scientecheasy.com web server. This request is known as an HTTP (Hypertext Transfer Protocol) request, which lets the web server know which file is being requested.
In other words, when the user requests a static web page, the browser sends an HTTP request to the web server that includes the file name that’s being requested. Look at the below figure to see a simple HTTP request.
HTTP is a communication standard which governs the requests and responses taking place between the user’s computer browser and the web server. The task of a web server is to accept a request from the client and serve up a requested web page to the client in a meaningful way. Therefore, the term server is used.
A “client” is a device or software application that requests services or resources from a web server. Clients can be user’s personal computers, smartphones, tablets, or any other device that connects to a network. They use web browsers to communicate with servers and access the resources or services provided by those servers.
A web server is a server that can share resources such as websites, files, databases, e-mail, and printers. The user either types the URL (address) of the web page into the browser’s address bar or clicks a link on the current page that specifies the next page to load. In both cases, the web browser makes a request for the web page and sends it to the web server.
3. HTTP Response: Sending Web Pages from Server to Browser
When the web server receives the HTTP request, it fetches the requested web page from the hard disk drive and sends it back to the browser as an HTTP response. This response includes the HTML document or file that was requested for displaying. Look at the below figure where you can see the HTTP response for a simple web page, which includes the HTML for the page.
4. How Browser Renders Web Pages for Users
When the web browser receives the HTTP response, it uses the HTML to format the page and displays the web page in the browser. Then, the user can view the content. If the user requests for another page, either by clicking a link or entering another web address in the address bar of the browser, the process starts again.
Two protocols that web applications depend upon
1. HyperText Transfer Protocol (HTTP) is the protocol that web browsers and web servers use to communicate with each other. It sets the specifications for HTTP requests and responses.
2. Transmission Control Protocol/Internet Protocol (TCP/IP) is a suite of protocols that allow two computers to communicate over a network.
How PHP Works with Server for Dynamic Web Pages?
PHP is an open source, popular, and widely used as server-side scripting language for the web development. It is well suited for developing the dynamic web pages or dynamic websites.
Dynamic web pages are those pages that change according to conditions. For example: When I log in to my Instagram account, I see my content. When you log in to your Instagram account, you see your content.
Here, we are loading the same resource (www.instagram.com), but we would be served different content dynamically. This would be impossible with static web pages (i.e. HTML web pages), because they are static, meaning they can’t change. Every user would see exactly the same HTML web page.
Dynamic web pages allow web developers to create interactive web applications. As a result, users can purchase goods and services, search the information from web, and communicate with other users through social networking sites, forums, and blogs.
PHP is designed for use with the HTML code. A PHP script is a file that contains the code written in the PHP language. This file has .php extension. The web server interprets the PHP code and usually generates an HTML code, which is sent back to the user’s browser.
Let’s understand the whole process in detail about how PHP works with a web server to process dynamic web pages.
There are the following steps through which PHP works with a web server to process the dynamic web page. They are as:
1. Browser Looks up the IP Address
The process starts when the user enters http://www.scientecheasy.com into his browser’s address bar, the browser looks up the IP address for scientecheasy.com.
2. Browser Sends HTTP Requests to Server
When the web browser issues an HTTP request for the home page of scientecheasy, the request crosses the Internet and reaches at the scientecheasy.com web server.
The browser issues a request when the user either types the URL of the home page in the address bar of the browser, click a hyperlink to load the dynamic page, or click a button to submit a form that contains the data.
In each case, the web browser issues an HTTP request and sends it to the web server. If the user submits a form that contains data, the HTTP request adds that data.
3. Web Server Processes HTTP Requests with PHP
The web server receives the HTTP request, it fetches the requested home page from its hard disk. Since the home page is now in the memory (RAM), the web server will notice that this file is incorporating PHP scripting. Then, the web server passes the request to the PHP interpreter, which is running on the web server.
4. Executing PHP Scripts and Interacting with the MySQL Database Server
The PHP interpreter executes the appropriate PHP script from the hard drive. Some of the PHP script contains MySQL statements, which the PHP interpreter now forwards to the MySQL database server.
5. Organizing and Retrieving Data with a Database Server
A database server stores information or data that is organized in tables. This data can be quickly retrieved through the database query.
6. Returning MySQL Query Results to the PHP Interpreter and Web Server
The MySQL database sends the results of the queries back to the PHP interpreter. Then, the PHP interpreter returns the outcome of the executed PHP code, along with the results from the MySQL database, to the web server.
7. Sending the Dynamically Generated Web Page to the Browser via HTTP
When the PHP interpreter completes the processing of the PHP script, it passes the dynamically generated web page back to the web server. Then, the web server sends the page back to the web browser in an HTTP response that includes only the HTML code for the web page.
8. Browsers Display Web Pages from HTTP Responses
When the web browser receives the HTTP response, it formats and displays the HTML web page. This is called rendering a page. Note that the browser cannot tell whether the HTML in the HTTP response was for a static web page or a dynamic page generated by the PHP interpreter because all it receives is HTML.
9. User Interaction with Content
When the browser displays or renders the HTML web page that it receives, the user views the content on the browser. This process starts again when the user requests for another page.
Note that the web browser cannot execute the PHP code at all. This is because the PHP code is processed by a machine. This machine is referred to as the web server. The server processes all the PHP code in the file before sending the document to the user’s browser. Therefore, it is a server-side scripting language.
In this tutorial, you have learned how PHP works with a web server to process dynamic web pages. You also understood how a web server processes the static web pages. The process of static pages is straightforward, while dynamic pages involve additional steps like executing PHP code and interacting with databases.
Always remember that the PHP code is executed on the web server, not on the client browser. Only the output is sent to the client’s browser as an HTML web page due to the security.