PHP is one of the most common programming languages. Created for writing small home pages, it has gradually grown and is now used by millions of websites. However, the matter is not limited to Web sites – it can be used in almost any application, good thing there is an interactive, CGI and FastCGI modes.

CGI mode, I want to describe. Its advantages are its relative simplicity and the ability to pass different data (including binary) to scripts. Its only disadvantage is the speed, because of the multiple runs of the application. This drawback, however, can be fixed with the newer FastCGI protocol.

First, let’s define the Common Gateway Interface (CGI). In fact it is just a startup of an application with defined environment variables. Since this is, after all, a web technology, the variables will be closely related to the HTTP protocol. Let’s look at RFC 3875 and see what environment variables are needed for CGI/1.1:

  • AUTH_TYPE – type of HTTP authorization (basic, digest or other) Can be empty.
  • CONTENT_LENGTH – length of data passed in POST or PUT request. Usually taken from the Content-Length header of the request.
  • CONTENT_TYPE – type of data passed in POST or PUT request. Normally taken from the Content-Type header of the request.
  • GATEWAY_INTERFACE – CGI version used. Usually “CGI/1.1”.
  • PATH_INFO – part of the path after the CGI application path, but before the request variables. For example, for the query “/somewhere/cgiapp.exe/test/?qwerty=123” it will be “/test/”.
  • PATH_TRANSLATED is the same as PATH_INFO, but projected onto the filesystem. For example, “./htdocs/test/”.
  • QUERY_STRING – query variables (without the question mark). For example, “qwerty=123”.
  • REMOTE_ADDR – IP address of the remote user.
  • REMOTE_HOST – The domain name of the remote user. Usually it is equal to REMOTE_ADDR.
  • REMOTE_IDENT is a user identification data according to RFC 1314. It is rarely used.
  • REMOTE_USER is remote user name that passed HTTP authentication.
  • REQUEST_METHOD – request type (GET, POST, PUT, etc.)
  • SCRIPT_NAME – request part with path to CGI application. For example, “/somewhere/cgiapp.exe”.
  • SERVER_NAME – server name (host name or domain name).
  • SERVER_PORT – the port on which the server is running.
  • SERVER_PROTOCOL – the protocol used by the server. Usually “HTTP/1.1”.
  • SERVER_SOFTWARE is the version of the server. For example, “MyServer 1.0”.

For POST and PUT requests the request data is passed to the standard input of the application.

However, all these variables are only desirable but not necessary to start php-cgi. The only mandatory environment variable is SCRIPT_FILENAME which is not mentioned in the RFC. It must contain the path to the PHP script on the file system, e.g. “/home/some-user/htdocs/test.php”. To run a PHP script via CGI in your application, you just need to specify it and run php-cgi (not “php”, exactly “php-cgi”!), redirecting its standard input and output.

If you’re not writing your own web server, you need to trim the PHP output headers. When capturing output, wait for the first blank line (just two line breaks in a row, “\n\n”) and use only what comes after it.

PHP in CGI mode is desirable to use only when you want to work with scripts not very actively, passing them any data or variables. If you need to actively work – use FastCGI, and if you do not need to pass anything to the script – it will be easier to run the PHP interpreter, passing it as an argument to the path to the script.