C++ with CGI

We can use CGI, Common Gateway Interface to display the output of C++ program on browser.

There are two methods to do it.

1. Without a Script.

2. With a Script.

Method1:

Here is C++ program that need to be placed in cgi-bin folder. (priya.cc)

#include <iostream>
 using namespace std;
 int main(void)
 {
 cout<<"Content-type: text/html";
 cout<<endl<<endl ;
 cout << "Hello GD " << endl ;
 return 0;
 }

To run the program:

sudo -s
cd /usr/lib/cgi-bin
g++ priya.cc 
This will create an executable file a.out. 
Now open the browser and type in the URL 
http://localhost/cgi-bin/a.out

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

Method2:

Here is C++ Code: (priya.cc)

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

Here is script: (priya)

#!/bin/sh
echo Content-type: text/html
echo
echo "<pre>"
./a.out
echo "</pre>"

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

How to execute the code?

Go to terminal. Type

sudo -s
cd /usr/lib/cgi-bin
g++ pp.cc
firefox http://localhost/cgi-bin/priya

And you will get output of C++ on browser.

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

How to take input of program from user while using CGI?

For this we need a html form also, which takes input from user.

Here is Html code: (table.html)

<body bgcolor = #d0e4fe>
 <form action="http://localhost/cgi-bin/table" method="GET">
 <font color = red size = 6> <center>Create a Table</font><br/><br/><br/>
 Enter any number : <input type="text" name="fn"/>
 <input type="submit" value = "Submit" /></center>
 </form>

Here is C++ program (table.cc)

#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;

main(int argc, char **argv )
{
int fn = atoi ( argv[1] );
cout<<"<html><head><title> Table </title></head><body bgcolor = #d0e4
fe>\n";
cout<<"<h1><font color = red size = 6><center> Table Of "<<fn<<"</font>
</center>\n";
cout<<"<table bgcolor = white border =red width = 5 align = center>
<tr>\n";
for (int i =1; i<=10; i++)
{
int c= fn*i;
cout<<"<td>"<<fn<<"<td>*<td>"<<i<<"<td>=<td>"<<c<<endl;
cout<<"</tr>\n";}
cout<<"</table></body>";
return 0;
}

Here is script: (table)

#!/bin/sh
echo Content-type: text/html
echo
echo '<html> <head> <title> CGI script </title> </head> <body>'
       fn=`echo "$QUERY_STRING"  | sed "s|fn=||"`
echo "<pre>"
./a.out "$fn"
echo "</pre>"

Place C++ program and script in /usr/lib/cgi-bin and html program in /var/www

Go to terminal:

sudo -s
cd /usr/lib/cgi-bin
g++ table.cc
firefox http://localhost/table.html

Give the value in form and press submit button. Script will run and give the output on browser using html

How to take character input from user in CGI with C++?

The HTML code is: (char.html)

<html>
 <head><link rel="stylesheet" href="css.css" type=text/css /></head>
 <title>Passing characters from HTML to C++ via CGI</title>
 <form action ="http://localhost/cgi-bin/char" method = "GET">
 Enter your name : <input type ="text" name = "name"><br/>
 <center><input type = "submit" value= Submit></center>
 </form>
 </html>

Script is: (char)

#!/bin/bash
echo Content-type: text/html
echo
name=`echo "$QUERY_STRING" | sed "s|name=||"`
echo "<pre>"
./a.out "$name"
echo "</pre>"

C++ Program is:

#include<iostream>
 #include<stdio.h>
 #include<stdlib.h>
 using namespace std;
 int main(int argc,char *argv[])
 {//const char * name=getenv("name");
 cout<<"Name is= "<<argv[1];
 return 0;
 }
Advertisements

About Priyanka Kapoor

Simple, Hardworking & friendly.....
This entry was posted in Uncategorized. Bookmark the permalink.

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