Introduction to CGI in C++

Try to find a free unix account online  or  go to AV lab next week

 

Example HTML form:

<html>

<form action=cgi-bin/ebrahimi.cgi method="post">

First Name: <input type=text name=fname size=10><br>

Last Name: <input type=text name=lname size=10><br>

<input type=submit value=submit>

</form>

</html>

 

Sample CGI class:

#include<string>
#include<iostream>
using namespace std;

class
Cgi{
        string env;
    public:
       
Cgi(){
           
cin>>env;
           
cout<<"Content-type: text/html\n\n";
 }

    string getStr(string name){
        int i;
        int loc;// = npos;
        loc = env.find(name);
        loc = loc + name.length() + 1;
        char s[100];
        for(i=0; env[loc]!='&' && env[loc]!=0; i++){
            s[i] = env[loc];
            loc++; }
        s[i]=0;
        return s; }
    };

int main(){
   
Cgi myCgi;
    string n;
    n =
myCgi.getStr("fname");
    cout<<n;
    return 0;
}
 

Sample output as HTML embedded in C++:

 

cout<<"Content-type: text/html\n\n";

cout<<"<HTML><B>Your Name: </B>";

cout<<fname<<" "<<lname;

cout<<"</HTML>";