| Header file: | cgi.h |
| Object name: | N/A |
| Object index: | N/A |
The CGI header is a group of functions designed to make programming CGI programs a snap. It handles the parsing of the query string and makes retrieving the values easy. It can read from GET or POST methods.
Note: this header file also compiles and works under Linux. You will require the bstring class for Linux as well.
The best way to explain the use of this header file is by a demonstration:
#include "bstring.h"
#include "cgi.h"
int main(int argc, char** args[])
{
//Firstly, Read in the CGI arguments and parse them.
//This is the way to do it if you are using the GET method...
int NumberOfArguments = GetCGI_GET();
//And if you are using the POST method...
int NumberOfArguments = GetCGI_POST();
//The number returned from the above functions is the number of
//arguments passed to your CGI program. You can use this as a check
//to see if your program was called correctly.
//Now, print out the headers...
printf("Content type: text/html\n\n");
//And then whatever you want...
//Lets say the program is passed the arguments:
// Name=Person&Surname=Person_Sur.
//Here's how we greet them:
printf("<HTML><BODY>");
printf("<H2>Hello there %s %s!</H2>", CGIArgs.GetValue("Name"),
CGIArgs.GetValue("Surname"));
printf("</BODY></HTML>");
//Let's say that you would like to get all the arguments without knowing their name.
//Since F3C 1.03 you can - like this...
for (int TempX = 0; TempX < CGIArgs.GetNoValues(); TempX++)
{
... = CGIArgs.GetValueName(TempX);
... = CGIArgs.GetValue(TempX);
}
//Now that wasn't too painful, was it?
//So there you can see that we extract the value that we want by
//calling CGIArgs.GetValue(). It always returns a string, so you
//might need to convert it to an integer or number sometimes.
//Ok, there are a few other things you should know about:
//Firstly, you can check if a value exists by calling
// CGIArgs.DoesValueExists("value_name");
//It will return TRUE if it does, or FALSE otherwise.
return 0;
};
To compile these programs is easy - just be sure to compile it as C++, and a CONSOLE application. Under Linux, you'd compile with g++ instead of gcc (although compiling with gcc might still work).
If you can't seem to return a value from CGIArgs.GetValue(), remember that it is case sensitive.
If you are developing scripts for Apache, it may give you 500 server errors when you try to run it. Nobody is sure what the problem is, however, you can get past it by regularly calling fflush(NULL); - which empties the console buffer. You especially should call it after the line, printf("Content type: text/html\n\n");.
Other than that, have fun CGI scripting!
| Back to index | The FreeFoote Foundation Classes Documentation |