What is CGI? - Images: Japanese
Hey there, everyone!

In this article, we'll take a look at 'CGI' (Common Gateway Interface) and use it as a jumping-off point to trace through how PHP actually processes a request. "CGI" is short for "Common Gateway Interface" — a term you don't hear quite as often these days, but it's still worth knowing.
Put simply, 'CGI' refers to the mechanism behind a 'dynamic website' — the kind built with PHP and similar technologies. Plain HTML-only websites, by contrast, don't do much in the way of dynamic behavior, so they're usually called 'static websites'.
More precisely, 'CGI' refers to the system that allows a 'web server software' to run a 'programming language'. Throughout the previous articles, we've been running 'MAMP' (Apache) while executing PHP code alongside it. So everything we've built so far qualifies as a proper 'CGI' setup.
Dynamic websites are so commonplace now that it's easy to shrug and think "it's just a website built with PHP, nothing special." But back in the day, a website that could change its content based on how users were interacting with it was genuinely revolutionary — that's exactly why this kind of system got its own name: 'CGI'. Back then, websites made of plain HTML were the norm.
With that context in mind, let's walk through how PHP processes a request, step by step. Imagine you place the following plain HTML file on some web server, saved as 'index.html'.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP Test</title> </head> <body> This is a test! </body> </html>
Say you place that file in the root directory / of our wp-p.info web server. When you type http://wp-p.info into your browser's address bar, the server sends back the following text file (HTML file):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP Test</title> </head> <body> This is a test! </body> </html>
Yep — it comes back exactly as written. That's how a web server without 'CGI' handles things.
Now let's try combining a 'web server software' with a dynamic language. In our case, that's 'Apache' and 'PHP'. This time, place the following file in the root directory '/' of the 'wp-p.info' web server, saved as 'index.php'.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP Test</title> </head> <body> <?php echo 'This is a test!'; ?> </body> </html>
Now when you type http://wp-p.info into your browser's address bar, the server sends back this text file:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP Test</title> </head> <body> This is a test!</body> </html>
Notice that the PHP code has been processed and the result is included in the returned text file. That's how a 'CGI'-enabled website — a 'dynamic website' built with PHP — handles a request.
In this example we're just outputting a fixed string ("This is a test!"), so it doesn't feel particularly dynamic. But write a program that shows the current time, or the number of active visitors, and suddenly each request can return a different page depending on the situation. There's a lot you can do with this. It all seems obvious now, but that kind of behavior was once genuinely groundbreaking.
To summarize the full flow when a simple PHP file receives a request:
- A browser somewhere sends a request for a PHP file.
- The web server detects the request and generates a text file based on the PHP code in that file.
- The generated text file is sent back to whoever made the request.
- The browser renders the returned text file and displays it as a web page.
'Request' literally means "a demand" or "an ask." In programming, it refers to asking another system to provide specific data or perform a specific action.
As a quick example: when you type http://wp-p.info/index.php into your browser's address bar, we'd say "the browser is sending a request to /index.php on wp-p.info."
From the web server's perspective, it's like: "A request just came in for /index.php! I'd better process the PHP and send a response back!"
'Request' is a term you'll hear a lot in professional settings — things like "configure this endpoint to return JSON when it gets a request" or "I'm worried the server will buckle under a high volume of requests." It's worth getting comfortable with it.
When the web server detects a request for a PHP file, it runs the code written in that file. In other words: when a PHP file is requested, the PHP code inside it executes.
The key insight behind 'CGI' is that PHP processing is triggered when a PHP file is requested. The 'web server software' (Apache) is what detects the request and kicks things off, while the dynamic language (PHP) is what actually does the processing. Keep that distinction in mind.
And that covers CGI and the PHP request flow. Hopefully you've got a sense of how the whole thing fits together.
In the next article, we'll dig into 'strings'. See you there!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.