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.
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.
//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 id="query-nbsp-string-nbsp-is-nbsp-nbsp-nbsp-queryString-nbsp-nbsp">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 id="query-nbsp-string-nbsp-is-nbsp-nbsp-queryString">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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft