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:
#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;
}
- #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;
- }
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
Ozzu Hosting - Want your website on a fast server like Ozzu?