C++ with CGI

There are basically two methods are used to work with it:

1. Without a script
2. With Script

EXPLANATION:

1. WITH A SCRIPT:

First of all write the C++ program that to be placed in cgi-bin folder (simran.cpp)

#include<iostream>
using namespace std;
int main(void)
{
cout<<endl;
cout<<"Hello Friends"<<endl;
return 0;
}

To run the program:

sudo -s
cd /usr/lib/cgi-bin
g++ priya.cc
This will create an executable file of the program. Now open the browser and type the URL

http://localhost/cgi-bin/a.out

You’ll see the output.

Things to be remember:
One addition break is required below line Content-type: text/html. First endl is to insert break in line Content-type text/html and second endl is to insert break below content line (i.e one blank line below content line). But in script , we need only one echo below content line as shell script doesn’t require to break each line manually.

2. WITHOUT SCRIPT:

C++ Code:(simran.cpp)

#include<iostream>
using namespace std;
int main(void)
{
cout<<"Hello Friends"<<endl;
return 0;
}

Script:(simran)

#!/bin/sh
echo Content-type: text/html
echo
echo "

"
./a.out
echo "

"
Place both C++ program and script in usr/lib/cgi-bin folder.

EXECUTE THIS: Open the terminal and type:

sudo -s
cd /usr/lib/cgi-bin
g++ simran.cpp

give the address: http://localhost/cgi-bin/ and you’ll see the output.

Things to be remember:
Extra echo is required after echo Content-type: text/html line. Other Cgi-bin can’t determine that is to be displayed on browser using CGI. Make sure that script has executable permissions.

That’s all.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s