Home  >  Article  >  Backend Development  >  What is the difference between cgi and php?

What is the difference between cgi and php?

青灯夜游
青灯夜游Original
2020-08-19 15:45:432801browse

Difference: CGI is a protocol and standard, a specification, not a language; following this standard, you can write programs in various languages ​​(including PHP) to process web page requests and return data to the client browser. PHP is a dynamic web development language. PHP can work in CGI mode or in modules such as ISAPI and NSAPI.

What is the difference between cgi and php?

CGI is a universal gateway protocol and a standard for developing dynamic web pages. If you follow this standard, you can use BAT, CMD, sh, Programs written in languages ​​such as PERL, C, C, PERL, and PHP process web page requests and return data to the client browser. CGI is a protocol and standard, a specification, not a language.

cgi is an interface, no matter what language is used, it can be implemented according to this interface. Generally, before the server determines that it needs to call the cgi program, it will place the requested GET parameters in the environment variable QUERY_STRING, and the POST request content will be transmitted to the cgi program through the standard input stream. The cgi program only needs to print the HTTP protocol (including HTTP headers and HTTP Body) to the standard output stream stdout, and the server will transmit them directly to the browser.

//c++实现cgi接口,打印query string和post data
#include <iostream>
 
using namespace std;
 
int main() {
    cout << "Content-type:text/html\n\n";
    const char *queryString = getenv("QUERY_STRING");
    if (queryString != NULL) {
        cout << "<h1>query string is :" << queryString << "</h1>" << endl;
    } else {
        cout << "<p>No query string</p>" << endl;
    }
     
    string postData, tmpData;
    while(cin >> tmpData) {
        postData += tmpData;
    }
    if (postData != "") {
        cout << "<div> " << postData << " </div>" << endl;
    } else {
        cout << "<p>No post data</p>" << endl;
    }
     
    return 0;
}

Compile it, name it test.cgi, and then throw it to the server into the cgi directory. Then access a URL similar to http://localhost/cgi/test.cgi?a=b&c=d, the server will put a=b&c=d in the QUERY_STRING environment variable and pass it to test.cgi. Finally, you can see that query string is a=b&c=d is displayed on the page. You can also create a form and POST data to http://localhost/cgi/test.cgi, and then you can see all POST data displayed on the page.

PHP is a dynamic web development language, mainly used to process data submitted by the browser and return results to the browser. PHP can work in CGI mode or in modules such as ISAPI and NSAPI.

Writing a CGI program in PHP:

#!/usr/env php
<?php
echo "Content-type:text/html\n\n";
 
$queryString = $_ENV("QUERY_STRING");
if ($queryString != NULL) {
    echo "<h1>query string is : $queryString</h1>";
} else {
    echo "<p>No query string</p>";
}
 
$postData = file_get_contents("php://stdin");
if ($postData != "") {
    echo "<div> $postData </div>";
} else {
    echo "<p>No post data</p>";
}
?>

Okay, its function is exactly the same as the program written in C above. Let’s add the “executable attribute” to it chmod x testphp. cgi then throws it into the cgi directory, then accesses http://localhost/cgi/testphp.cgi?a=b&c=d, and uses form post data to it. The effect you see should be the same.

So you have also seen that when the server calls a CGI program, it doesn't care what you use to implement it. The interface is already set anyway, so you just need to program according to the interface.

But generally the logic of the server calling PHP and calling CGI programs are different. The CGI program needs to parse QUERY_STRING and POST_DATA by itself. PHP should interact with the server through another set of extensions. Therefore, when writing web pages directly with PHP, the way of writing web pages is really different from usual. For example, the server's PHP The extension has helped us parse the query string and post data into arrays. We can just get the values ​​directly:

<?php
 
if (isset($_GET)) {
    print_r($_GET);
} else {
    echo "<p>No query string</p>";
}
 
if (isset($_POST)) {
    print_r($_POST);
} else {
    echo "<p>No post data</p>";
}
?>

Save it as test.php and then visit http://localhost/test.php?a=b&c =d, or form submission content to the page. You can see that the output data is structured and has become an array. Moreover, we no longer need to output the HTTP header ourselves (except in special circumstances).

For more related knowledge, please visit:

PHP Chinese website!

The above is the detailed content of What is the difference between cgi and php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to unescape in phpNext article:How to unescape in php