search
HomeBackend DevelopmentPHP ProblemWhat is the difference between cgi and php?

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 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!

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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

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

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

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.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

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.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

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

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

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

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

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.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

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

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft