Tutorial: CGI programming with C/C++

Discussion in 'Web Design & Programming' started by Addis, Aug 24, 2006.

  1. Addis

    Addis The King

    Likes Received:
    91
    Trophy Points:
    48
    CGI is a web browser technology which stands for Common Gateway Interface. In essence, CGI is really nothing of a new technology, like AJAX it uses existing technologies to do something useful.

    CGI is used on websites sometimes to generate dynamic content, or do complex operations not possible or harder to achieve in a high level language like PHP or ASP. You might have come across this while browsing the web, and it is still a popular with some developers (others call them masochists).

    When you go to a web page in the form of a CGI script, instead of the web browser sending the html file, or if its PHP passing it through the PHP interpreter, the web browser actually runs the CGI script, and what you see is the output.

    CGI isn’t itself a language though, it actually represents any program which runs with the web browser to send data to the user when invoked. The language used to write the program can be compiled like C or C++, or it can be an interpreted language like Perl which is often the case.

    In this example I’m going to be using C++, but if you want you can use C instead and use the stdio header functions instead of the iostream constructs.

    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
    cout << “Content-type: text/html\n”; 
    cout << “<html><head><title>C++ cgi!</title></head>\n”;
    cout << “<body>First line!<br />\n”; //note that \n isnt visible to user
    cout << “Second line!</body></html>\n”; //finish page
    return 0; //exit program
    }
    
    Compiling details

    To most users out there, I would say that you can’t write/compile the CGI program first and upload it to the server. This is because most users run Windows, and most web servers run Linux and Apache. The resulting binary will be for a Windows platform, so won’t run on a Linux server.

    However, if you know what type of system your web server has, and you use a system that closely matches it then you may not have a problem. For example, I compiled the program and uploaded it to my server using FTP, it works because I wrote/compiled it on a Linux system, and the web server also runs Linux.

    If you have a different OS to your server, then you’ll need ssh access to your web server account to run commands, or some other way to invoke the compiler installed on your server and produce an output.

    Other languages

    Of course, you’re not tied down to C/C++ for writing CGI programs, you can use an interpreted language like Perl or Python instead, whatever you see fit for the job. What are the advantages of writing user web pages in C/C++? Well it has the same advantages as the language itself, its fast. Very fast, while normally this isn’t noticed, if you’re writing a program which will do complex calculations or other operations with a lot of users then the extra time in writing/debugging the code may pay off.

    Hope this has been helpful in understanding CGI. As a forum member called ThePenguinCometh once said, “you wouldn’t write a web page in C”, well it has been done, and it has uses. :)

    Update coming soon...
     
  2. Addis

    Addis The King

    Likes Received:
    91
    Trophy Points:
    48
    Execute permissions

    You may come across some problems if you try and run a CGI script on a *nix server. One possible cause of this is that your program doesn't have execute permissions and so can't be executed.

    To change the file permissions to set the file execute bit on, connect to your server with an ftp client and use the GUI tool to change the permissions. If you're using a command line client, like GNU FTP, then you can use basic shell commands on the FTP files. So you can do
    Code:
    chmod a+x myfile.cgi
     

Share This Page