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, business is not limited to websites - it can be used in almost any application, the benefit is interactive, CGI and FastCGI modes.
CGI mode I want to describe. Its advantages are in relative simplicity and the possibility of transferring various data (including binary) to scripts. He has only one minus - the speed of work, due to repeated launches of the application. However, this minus is corrected with the help of the newer FastCGI protocol.
First, let's define what the Common Gateway Interface (CGI) is. In fact, this is just a launch of an application with certain environment variables. Since this is still a web technology, then the variables will be closely related to the HTTP protocol. Let's look at
RFC 3875 and see what environmental variables are needed for CGI / 1.1:
- AUTH_TYPE - HTTP authorization type (basic, digest, etc.) Can be empty.
- CONTENT_LENGTH - the length of the data transmitted in the POST or PUT request. Usually taken from the header Content-Length request.
- CONTENT_TYPE - the type of data transmitted in the POST or PUT request. Usually taken from the header Content-Type request.
- GATEWAY_INTERFACE is the version of CGI used. Usually "CGI / 1.1".
- PATH_INFO - part of the path after the path to the CGI application, but before the query variables. For example, for the query "/somewhere/cgiapp.exe/test/?qwerty=123" this will be "/ test /".
- PATH_TRANSLATED is the same as PATH_INFO, but projected onto the file system. For example, "./htdocs/test/".
- QUERY_STRING - query variables (without question mark). For example, "qwerty = 123".
- REMOTE_ADDR is the remote user's IP address.
- REMOTE_HOST is the domain name of the remote user. Usually equal to REMOTE_ADDR.
- REMOTE_IDENT - user identification data according to RFC 1314 . Used rarely.
- REMOTE_USER - the name of the remote user who has passed HTTP authorization.
- REQUEST_METHOD - request type (GET, POST, PUT, etc.)
- SCRIPT_NAME - part of the request with the path to the CGI application. For example, "/somewhere/cgiapp.exe".
- SERVER_NAME - server name (host name or domain name).
- SERVER_PORT is the port on which the server is running.
- SERVER_PROTOCOL is the protocol used by the server. Usually "HTTP / 1.1."
- SERVER_SOFTWARE - server version. For example, “MyServer 1.0”.
For POST and PUT requests, the request data is transferred to the standard input of the application.
However, to run php-cgi, all these variables are only desirable, but not necessary. A mandatory environment variable is only one, and even then not specified in the RFC - SCRIPT_FILENAME. It should contain the path to the PHP script in the file system, for example "/home/some-user/htdocs/test.php". To run a PHP script through CGI in your application, you just need to specify it and run php-cgi (not “php”, namely “php-cgi”!), Redirecting its standard input and output.
If you do not write your web server, you need to trim the output PHP headers. Intercepting the output, wait until the first empty line appears (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 it is necessary not to actively work with scripts, passing them any data or variables. If you need active work, it is better to use FastCGI, and if you don’t need to pass anything to the script, then it will be easier to start the PHP interpreter, passing the script path to it as an argument.
And not to be unsubstantiated, a small example of work (Windows, C #):
using System; using System.Diagnostics; namespace PhpApp { class Program { static void Main(string[] args) { Process php = new Process();
