Guide

How PHP Works on the Server: Request and Response Explained

Understand how PHP works on a web server: the request and response cycle, web server and PHP engine roles, and how output reaches the browser.

Key takeaways

  • PHP runs on the server, and the browser receives only the output, which is why view source never shows PHP code.
  • A request travels from the browser to the web server, then to the PHP engine, and the result returns the same way.
  • Data reaches a script through the address, form fields, headers and cookies; GET suits lookups and POST suits actions that change something.
  • Sessions let a site remember a visitor between requests by keeping a record on the server and an identifier in the browser.
  • On a live site, hide detailed errors from visitors and write them to a log instead.

How does a PHP page reach the browser?

When you open a website that is built with PHP, a conversation takes place between your browser and a computer called the server. Your browser sends a request, the server does some work, and then it sends back a response. PHP is the part that does the work on the server side. Understanding this simple cycle is the most important step in learning how dynamic websites function.

A static page is a file that is sent exactly as it is stored. A dynamic page is built at the moment someone asks for it. PHP helps in this second case, because it can read data, make decisions and produce a fresh page for each visitor.

What happens when you request a PHP page?

  • You type an address or click a link, and the browser sends an HTTP request to the server that hosts the site.
  • The web server software, such as Apache or Nginx, receives the request and looks at the address to decide what to do.
  • If the address points to a PHP file, the web server passes the request to the PHP engine.
  • The PHP engine reads the file, runs the instructions, and may talk to a database or read other files while doing so.
  • The result of this work, usually HTML text, is handed back to the web server.
  • The web server sends the response to your browser, which then displays the page.

The important point is that the browser never receives your PHP instructions. It only receives the output. This is why you can use the view source option in a browser and still not see the PHP logic behind a page.

What does each component do?

The browser

The browser is the client. It sends requests, receives responses and renders HTML, styles and scripts. It does not run PHP.

The web server

The web server listens for incoming requests. It serves static files such as images and style sheets directly, and it forwards requests for PHP pages to the PHP engine. Apache and Nginx are two widely used examples.

The PHP engine

The engine reads PHP code, turns it into an internal form that it can run, and executes it. Common setups connect the web server and the engine through a module or through a process manager. As a beginner you do not need to configure these parts deeply, because shared hosting and local development packages arrange them for you.

The database

Many PHP applications store information such as users, products and posts in a database. PHP sends a query to the database, receives the data, and uses it to build the page.

The four parts of a PHP site

PartRoleRuns PHP?
BrowserSends requests, receives responses and renders HTML, styles and scriptsNo
Web serverReceives requests, serves static files and forwards PHP pages to the engineNo, it passes requests on
PHP engineReads the code, runs it and returns the outputYes
DatabaseStores users, products and posts that PHP queriesNo

How does PHP know what the visitor wants?

Every request carries information. The address can include parameters, a form can send fields, and the browser sends headers and cookies. PHP makes these available to your script so that you can react to them. For example, a search page can read the keyword typed by the visitor and show matching results. A login page can read the submitted fields and decide whether to allow entry.

Two common methods for sending data are GET and POST. GET places the data in the address and is suited to simple lookups, such as searching or filtering. POST places the data in the body of the request and is the usual choice for forms that change something, such as registering an account.

How does a PHP site remember a visitor?

HTTP by itself does not remember who you are between two requests. To build features like login, PHP uses sessions. When a session begins, the server creates a small record for the visitor and gives the browser an identifier, usually stored in a cookie. On the next request the browser sends the identifier back, and PHP uses it to find the record. In this way the site can recognise a returning visitor without asking for a password on every page.

The life of a script

Traditional PHP follows a short-lived model. For each request, the script starts, does its work, sends output and ends. Memory used by the script is released afterwards. This model is simple to reason about and makes it hard for one visitor request to disturb another. It also explains why information that must last longer, such as a shopping cart, needs to be kept in a session, a database or another storage.

Output is not only HTML

Although HTML is the most common output, PHP can produce many formats. It can return JSON for a mobile application or a JavaScript front end, generate a downloadable file, send an image, or redirect the visitor to a different address by setting a response header. The format is decided by the headers and the content that your script sends.

Why should errors be logged and not shown?

When something goes wrong, PHP can report an error. On a development machine you want to see the messages on the screen to fix problems quickly. On a live site you should hide detailed messages from visitors and write them to a log file instead, because error text can reveal paths and other internal details. Learning to read logs is a valuable habit for every PHP developer.

Performance in simple terms

Because the engine does work for each request, speed depends on how much work is needed. Reducing database queries, caching results that do not change often, and keeping scripts tidy all help. Modern engines also keep compiled versions of scripts in memory so that files do not have to be processed again and again. You can leave advanced tuning for later, but the habit of asking how much work each page request does is useful from the beginning.

Local development

To practise, you can install a local package that includes a web server, the PHP engine and a database on your own computer. You write files in a project folder, open the local address in your browser, and watch the request and response cycle in action. This costs nothing and lets you experiment safely before you publish a site.

Recap

  • PHP runs on the server, and the browser only receives the output.
  • The web server hands PHP pages to the PHP engine.
  • Requests carry data through addresses, forms, headers and cookies.
  • Sessions allow a site to remember a visitor across requests.
  • Hide detailed errors from visitors on live sites and use logs instead.

Sources and further reading

  • PHP Manual: What is PHP?: describes PHP as a widely used, open source, general-purpose scripting language especially suited for web development and embeddable in HTML, and gives the recursive acronym PHP: Hypertext Preprocessor.
  • PHP Manual: $_GET: defines $_GET as the variables passed through the URL query string.
  • PHP Manual: $_POST: defines $_POST as variables passed by HTTP POST with form content types.
  • PHP Manual: Error Reporting: advises hiding errors from display in production and logging them instead.

Frequently asked questions

Does the browser run PHP code?

No. PHP runs on the server, and the browser only receives the output, such as HTML or JSON.

What is the role of the web server in a PHP site?

It receives requests, serves static files directly and passes PHP page requests to the PHP engine.

Why can I not see PHP code with view source?

Because the server runs the PHP code first and sends only the result to the browser.

How does a PHP site remember a logged-in user?

It uses sessions: the server keeps a record and the browser sends back an identifier, usually in a cookie.

All guides