Go Back   Hardware Forums > Software Support Forums > Web Development

Reply
 
LinkBack Thread Tools
Old 24-08-2006, 12:39 AM   #1 (permalink) Top
The King

 
Addis's Avatar
 
Join Date: Jan 2004
Age: 18 Male
Posts: 5,255
Times Helpful: 403
My Mood: Drunk
Status: Offline

My Computer

Default Tutorial: CGI programming with C/C++

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

__________________
Never trust a program you don't have the source code for.

My website | Powerful Desktop Linux | Linux for human beings | Linux for power users | Linux for ricers

Last edited by Addis; 25-08-2006 at 10:06 AM.
Send a message via MSN to Addis   Reply With Quote
Whats this? diNovo Edge Bluetooth Wireless Keyboard
diNovo Edge Bluetooth Wireless Keyboard
Seller Price (inc. VAT) Delivery Total Price Availability Seller Rating
Dell £114.33 Free £114.33 In Stock Rated: 0 out of 5 - Number of votes: 0
IT247.com £102.94 Free £102.94 In Stock Rated: 4 out of 5 - Number of votes: 38
Dell Business £104.65 £8.23 £112.88 In Stock Rated: 0 out of 5 - Number of votes:
Old 25-08-2006, 09:34 AM   #2 (permalink) Top
The King

 
Addis's Avatar
 
Join Date: Jan 2004
Age: 18 Male
Posts: 5,255
Times Helpful: 403
My Mood: Drunk
Status: Offline

My Computer

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
__________________
Never trust a program you don't have the source code for.

My website | Powerful Desktop Linux | Linux for human beings | Linux for power users | Linux for ricers
Send a message via MSN to Addis   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
good programming books willyfromnc IT Careers and Certification 4 28-07-2006 01:02 PM
Programming Help Sites and Tutorials Egaladeist Web Development 1 20-07-2005 02:05 PM
The Tao of Programming Addis General Chat 3 17-06-2005 08:19 PM
Java Programming Cerificate Mack Web Development 1 05-06-2005 06:58 AM
C programming harrack52 The War Zone 12 06-02-2004 04:11 PM


All times are GMT +1. The time now is 08:35 AM.


Copyright © 2000 - 2008 · HARDWAREFORUMS.COM · All rights reserved