Need help with CGI written in C

  • Katushai
  • Graduate
  • Graduate
  • User avatar
  • Posts: 152

Post 3+ Months Ago

hey, im new with CGI but i know C pretty well...i heard you can make a C program and put it on a website thru cgi, and i looked it up a little...but all i found was how to CHMOD and compile as a CGI...how do i get it on the website...i know alot of HTML, but i need cgi =/ well thanks in advance of course...=x
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

I just made a quick CGI application written in C/C++ for you to look at. Here is the working version I made. Click on this:

http://www.ozzu.com/examples/examplecgi ... =somevalue

Now the important part to any CGI C/C++ script is that you just output this before you start the output to the browser:

Content-Type:text/html\n\n

Then after that you can output things as you always do. So you can basically write the C scripts like you do now, just remember that if you want to output information to a browser you need to output that one line first. That is all it really takes to make it into a CGI application written in C or C++.

Also you should compile the script you make on the operating system that your website is on, or an identical O/S if you do not have permission to compile binaries or if its missing libraries that you need.

So all you gotta do once you have the binary is make sure it is on the webserver and with the right permissions. You need to chmod 755 on the program.

Here is the source code to the example program I made above:

Code: [ Select ]
#include <iostream>
#include <string>
#include <stdio.h>

int main()
{
  char buffer[128];      //buffer to hold getenv variables
  string requestMethod; //the request method such as POST or GET
  string queryString   //the query string of a GET post
  string httpReferer;  //the http referrer
  string httpUserAgent; //the clients user agent (browser)
  string remoteAddr;   //the clients IP address
 
  //get the Request Method from the visitor
  snprintf(buffer, 128, "%s", getenv("REQUEST_METHOD"));
  requestMethod = buffer;

  //get the Query String from the visitor
  snprintf(buffer, 128, "%s", getenv("QUERY_STRING"));
  queryString = buffer;

  //get the http referrer from the visitor
  snprintf(buffer, 128, "%s", getenv("HTTP_REFERER"));
  httpReferer = buffer; 

  //get the browser/user agent from the visitor
  snprintf(buffer, 128, "%s", getenv("HTTP_USER_AGENT"));
  httpUserAgent = buffer;
 
  //get the IP address from the visitor
  snprintf(buffer, 128, "%s", getenv("REMOTE_ADDR"));
  remoteAddr = buffer;

  //begin output to clients browser
  cout << "Content-Type: text/html\n\n";
  cout << "<html>" << endl << "<head><title>C/C++ CGI Test</title></head>" << endl << endl;
  cout << "<body bgcolor=\"#aaaaaa\">" << endl;
  cout << "Request Method: " << requestMethod << "<br>" << endl;
  cout << "Query String: " << queryString << "<br>" << endl;
  cout << "HTTP Referer: " << httpReferer << "<br>" << endl;
  cout << "HTTP User Agent: " << httpUserAgent << "<br>" << endl;
  cout << "Remote Address: " << remoteAddr << "<br><br>" << endl;    
  cout << "This is a small example CGI script written in C/C++<br>" << endl;
  cout << "</body>" << endl << "</html>" << endl;

  return 0;
}
  1. #include <iostream>
  2. #include <string>
  3. #include <stdio.h>
  4. int main()
  5. {
  6.   char buffer[128];      //buffer to hold getenv variables
  7.   string requestMethod; //the request method such as POST or GET
  8.   string queryString   //the query string of a GET post
  9.   string httpReferer;  //the http referrer
  10.   string httpUserAgent; //the clients user agent (browser)
  11.   string remoteAddr;   //the clients IP address
  12.  
  13.   //get the Request Method from the visitor
  14.   snprintf(buffer, 128, "%s", getenv("REQUEST_METHOD"));
  15.   requestMethod = buffer;
  16.   //get the Query String from the visitor
  17.   snprintf(buffer, 128, "%s", getenv("QUERY_STRING"));
  18.   queryString = buffer;
  19.   //get the http referrer from the visitor
  20.   snprintf(buffer, 128, "%s", getenv("HTTP_REFERER"));
  21.   httpReferer = buffer; 
  22.   //get the browser/user agent from the visitor
  23.   snprintf(buffer, 128, "%s", getenv("HTTP_USER_AGENT"));
  24.   httpUserAgent = buffer;
  25.  
  26.   //get the IP address from the visitor
  27.   snprintf(buffer, 128, "%s", getenv("REMOTE_ADDR"));
  28.   remoteAddr = buffer;
  29.   //begin output to clients browser
  30.   cout << "Content-Type: text/html\n\n";
  31.   cout << "<html>" << endl << "<head><title>C/C++ CGI Test</title></head>" << endl << endl;
  32.   cout << "<body bgcolor=\"#aaaaaa\">" << endl;
  33.   cout << "Request Method: " << requestMethod << "<br>" << endl;
  34.   cout << "Query String: " << queryString << "<br>" << endl;
  35.   cout << "HTTP Referer: " << httpReferer << "<br>" << endl;
  36.   cout << "HTTP User Agent: " << httpUserAgent << "<br>" << endl;
  37.   cout << "Remote Address: " << remoteAddr << "<br><br>" << endl;    
  38.   cout << "This is a small example CGI script written in C/C++<br>" << endl;
  39.   cout << "</body>" << endl << "</html>" << endl;
  40.   return 0;
  41. }


The commands I used to compile the script were:

[root@localhost html]# g++ -o examplecgi.cgi examplecgi.cpp
[root@localhost html]# chmod 755 examplecgi.cgi

Also you can download my source directly by going to this link:

http://www.ozzu.com/examples/examplecgi.cpp
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

I don't know C+ or C++ or VB or other programming languages very well but I was wondering if I could add a side comment? CGI is not a programming language. All cgi scipts we all know and/or use are comprised of programming languages. I think this is an important point to remember. I have found so far that most so-called CGI scripts are written in Perl, but I have seen them written in other languages.

It may or may not be appropriate for this thread, but I only became aware of that recently. Sorry for the intrusion, since I don't really have an answer to the problem in the thread itself. I just thought perhaps it might add to the discussion and solution.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Yup that is a good point ATNO/TW. Many people think CGI applications are like some special language, but really any programming language can be a CGI application as long as it outputs the right headers first to the browser.
  • Katushai
  • Graduate
  • Graduate
  • User avatar
  • Posts: 152

Post 3+ Months Ago

thanks.....another thing, is perl any different than C? aside from it being coded differently, as it is a different language...i mean, is the procedure to put it into cgi the same as C? i think i might have to buy a book on CGI.....but my mom wont give me the money for it, so maybe ill just hire somebody. anybody thats willing to make me a program in any language that does the job, email me daishi39@yahoo.com THERES MONEY INVOLVED
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

There's a few Key things to remember. If your program works -- it works...but you need to verify with the web host provider if it supports it. For example, I have had instances where the webhost prefers that the CGI script has an extension of .pl and others that prefer an extension of .cgi for the script.

You might need to do a bit of research and question asking with your webhost on that part.
  • Katushai
  • Graduate
  • Graduate
  • User avatar
  • Posts: 152

Post 3+ Months Ago

well thats the thing, im the webhost. im starting a webhosting company per say, and i need the code so that people can type their code into the box and hit submit, and itll save it on their own folder, on my computer, so itll appear on my website as a link.....so i have as much bandwidth and space as i need. i know it can be done cause my breother did it a long time ago, and he wont do it for me cause hes too busy. and i know how much its worth, or how much somebody would wnt to be paid for it, so im not going cheap or anything.....im willing to pay more than its worth, so please email me if you want to do the job...thanks
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

Ill leave that up to BWM then. Your current need is beyond my programming skills, but I think he can handle it. Good luck.
  • Katushai
  • Graduate
  • Graduate
  • User avatar
  • Posts: 152

Post 3+ Months Ago

thx...i have it so people can telnet in, but that would tuen people off so they wont wanna use my site. they dont wanna learn all those fancy commands....=P but see, my isp blocked my ports, so im gana fix that soon..... all i really need is something thatll save the html they type in to my server. and im willing to pay a decent amount of mula for it...by the way, whos BWM? Bigwebmaster?
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

BWM = Bigwebmaster -- Yep! He's the owner of this place.
  • Katushai
  • Graduate
  • Graduate
  • User avatar
  • Posts: 152

Post 3+ Months Ago

ok thx

Post Information

  • Total Posts in this topic: 11 posts
  • Users browsing this forum: No registered users and 125 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.