From the notes of week9 - lecture,

Once we’re in PHP mode, we access a variable named $_POST. This is an associative array which PHP constructs for you whenever you pass in data via the POST method. If we had used the GET method, the data would be available in the $_GET variable.

When we submit form data to a URL or a page using either of these methods (i.e., GET and POST), how does this web page receive this data underneath the hood? And for the PHP part, who is responsible for constructing these super global variables (i.e., $GET and $POST) and storing the submitted data in them?

2 Answers 2


Toward the end of the last century (early 1990's) a common way for web servers to talk to programming languages was by using the Common Gateway Interface (CGI). CGI is very simple and works in a way that is very similar to the C programs we create in CS50.

  1. The network connection is is handled by the web server, like Apache or Microsoft IIS. The web server accepts connections from web browsers, and waits for a request.
  2. The browser sends a request, which is just a few lines of text formatted according to the HTTP protocol. The request contains instructions like which file and web site to access on the server.
  3. If a request comes through for a file or an image, the web server will take care of sending the file to the client. In the case of a CGI program (like login.php), the web server will execute a text based program on the command line, passing through the text request sent from the browser through the standard input.
  4. The CGI program loads and runs like any normal program on the terminal. The main difference is that instead of the user entering a command to run the program, it is instead being run by the web server in response to a web request.
  5. The program will read input from the standard input (basically doing a getString()) just like a terminal program, except that the input text will be the request sent by the browser.
  6. The program will interpret (or parse) the input text, execute some instructions, print some output, and then exit. Instead of the output going to the terminal window it will be intercepted by the web server and sent back to the client.

The input text going into the program, and the response coming out of the program, would be formatted according to the HTTP specification. The HTTP specification describes some rules for formatting text and data so that browsers, web servers, and proxies can all treat the data in the same manner.

CGI has largely been superseded by more efficient technologies, such as ISAPI and FastCGI, although many web servers still support it. You can quite easily write a C program that interacts with a web server by using the same text handling functions covered in CS50.

When PHP was invented it followed the established convention of using CGI. There is an extra mechanism when using PHP since it is an interpreted language. What that means is that the PHP code itself does not run, but is instead run by the PHP interpreter. So PHP is a program that runs .php script files. Here's how it works:

  1. The server accepts an incoming connection from the browser.
  2. If the request is for a PHP script file, the web server will start up the PHP interpreter.
  3. The PHP interpreter will parse the incoming request from the client, and load the requested .php script file.
  4. The PHP interpreter will then execute each line in the script - examining each line of text in the script, converting it into instructions, and performing the resulting actions.
  5. Any outputted text or data are sent from PHP to the web server and back to the web browser.

So to answer your questions:

When we submit form data to a URL or a page using either of these methods (i.e., GET and POST), how does this web page receive this data underneath the hood?

The raw text data received by the server from the browser is passed to the PHP program. One way to think of it is like the pipe command | which is used in the find program in pset 3.

And for the PHP part, who is responsible for constructing these super global variables (i.e., $GET and $POST) and storing the submitted data in them?

In the simplest case, the PHP program automatically interprets the text data sent from the browser and populates the $_GET, $_POST, and $_REQUEST super globals. This usually happens automatically in a web server environment, and not automatically when PHP is executed from the command line.

In some circumstances the automatic behaviour is not desirable, such as when processing custom HTTP headers from the browser, it is possible to manually parse the raw text data inside the PHP script (see http_parse_headers as a starting point).

  • 1
    Now it's clear! Basically, the web server is running an interface that enables it to talk to programs running on it. It passes the input to a third-party program which, in turn, executes some PHP code (in this situation) that creates and sets/updates these super global variables. Please correct me if I'm wrong!
    – kzidane
    Aug 6, 2014 at 17:07
  • Correct, there's a lot going on behind the scenes, but that's the gist of it. Aug 6, 2014 at 21:35

When we submit form data to a URL or a page using either of these methods (i.e., GET and POST), how does this web page receive this data underneath the hood?

GET and POST are two request methods of the Hypertext Transfer Protocol (HTTP), which defines how the content we call a "web page" is transferred over the internet. On a more general level, internet traffic (not limited to web pages) is governed by a set of protocols known as TCP/IP. Together, these protocols define how computers communicate with each other as parts of the giant computer network we call the internet.

Your computer submits data to the web server in a request, and if the request is identified as a GET request, the web server treats it one way; if identified as a POST request, the web server treats it another way. Here's an example of what a simple GET request to load Wikipedia looks like "under the hood" on a client machine:

"Http request telnet ubuntu" by TheJosh - Own work. Licensed under Public domain via Wikimedia Commons.

The web server machine is simply another computer running software that implements the protocols as defined for a web server. You can set up pretty much any computer with an internet connection to serve web pages, it's simply a matter of the machine being able to listen for and respond to requests from other machines that are directed toward its unique address on the network.

So, the server receives GET and POST data in essentially the same way, but it recognizes which sequences indicate one or the other, similarly to the way that problem set 5 asks you to look for certain bytes in a big chunk of data that indicate the beginning of a JPEG file. Its software then takes the data and uses it to decide what actions to take in response. These actions could include sending you a different page, starting a file transfer, retrieving information from a database, storing information in a database, and so on.

And for the PHP part, who is responsible for constructing these super global variables (i.e., $GET and $POST) and storing the submitted data in them?

This is a more complicated question that I don't know if I can properly answer. Simply put, the web server software is designed in such a way that this request data is available to the PHP scripting environment. Another environment (e.g., JavaScript) might also have access to the GET and POST data, but not as global variables, or not by the same name. The reason you can get at this data so easily using PHP is simply that PHP has been developed specifically for the purpose of being easy to integrate into HTML.

If you're looking for a lower-level explanation like the one David gave in the earlier lectures about how C stores variables in memory, then you might be interested in the history of the PHP language. It turns out that PHP is implemented in C, so the mechanical storage of data "under the hood" takes place in effectively the same way that you learned at the beginning of the course.

  • So basically data are stored in databases on the web server which is accessible for PHP and JavaScript environments?
    – kzidane
    Aug 5, 2014 at 17:03
  • @Kareem The $_GET and $_POST variables are accessing some data that's loaded into memory. The data comes from the request, as shown in the image. Before that, the data might have come from user input, from a calculation in a function, from a file somewhere on the server, or any number of places. Databases are good structures for storing certain types of information, but not all information on the server is stored in a database.
    – Air
    Aug 5, 2014 at 17:26
  • I actually expected a more specific answer than that assuming that these super global variables are created and initialized at some point definitely by the web server. I just wanted to know whether the web server sort of executes some commands to do this job and how it does that (in case it already does). I know that the data is loaded in memory of course since it's stored in these variables, but I was asking how it was loaded in memory at the first place to access it using these variables (I'm talking about form data specifically (or user input more generally) since it's easy to imagine how
    – kzidane
    Aug 6, 2014 at 16:50
  • it was stored in these variables if it's returned from a function or loaded from a file). I'm sorry if I'm giving this more than it actually deserve. This was just a question that came out of curiosity. I may go deeper than that later. Thanks anyway!
    – kzidane
    Aug 6, 2014 at 16:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .