You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
REMOTE_HOST: Client's host name<br />
CONTENT_TYPE: Type <strong>of</strong> information transferred<br />
CONTENT_LENGHT: Number <strong>of</strong> bytes <strong>of</strong> data sent to the CGI by the client<br />
QUERY_STRING: Saves the data sent by the client if the transfer method is METHOD=GET. If it is<br />
METHOD=POST that is used, data will be read on the standard output.<br />
The difference with PHP lies above all in the way CGI processes the received data. PHP can<br />
recognize and directly use the name <strong>of</strong> variables defined in the form, something that the CGI program<br />
cannot do. The latter will have to recover everything that is after ? In the form <strong>of</strong> a character chain to<br />
process it, and so obtain the value <strong>of</strong> the variables that are sent to it through the form. CGI has to<br />
process arguments in this way because using a form always sends back data in the shape <strong>of</strong><br />
variable1=value1&variable2=value2... It can therefore be concluded that it would be entirely possible to<br />
pass the arguments to the program in a different form, by specifying them in the url after the ?, and<br />
naturally only if the program were coded in such way that it could process this information.<br />
Here is an example <strong>of</strong> code that will recover the information sent back by the client through a form:<br />
char buffer[50];<br />
buffer = getenv("REQUEST_METHOD"); // We recover the method used in the environment<br />
variable REQUEST_METHOD<br />
if (buffer) { // if this operation is successful we continue<br />
if (strcmp(buffer, "POST") == 0) { // If the POST method is used<br />
buffer = getenv("CONTENT_LENGHT"); // we recover the size <strong>of</strong> data sent back by the form<br />
if(buffer) { // If this operation is successful we continue<br />
length = atoi(buffer); // We put this value in digital form<br />
data = malloc(buffer + 1); // we attribute memory space for the data variable which will<br />
recover data<br />
fread(data, 1, length, stdin); // we copy what arrives on the standard output into buffer<br />
data[length] = '\0'; // we add the '\0' character at the end <strong>of</strong> the character chain<br />
contained in buffer<br />
}<br />
}<br />
else if (strcmp(buffer, "GET") == 0) { // But if the GET method is used<br />
buffer = getenv("CONTENT_LENGHT");<br />
if(buffer) {<br />
length = atoi(buffer);<br />
data = malloc(buffer + 1);<br />
}<br />
strcpy(data, getenv("QUERY_STRING")); // We copy the data contained in QUERY_STRING into<br />
buffer<br />
data[length] = '\0';<br />
}<br />
}<br />
Once the data sent back to the cgi program, it must be processed, in order to extract the variables'<br />
values: we know that these are separated with the & sign, and the following function could be in<br />
charge <strong>of</strong> recovering the value <strong>of</strong> the first variable in a temporary buffer.<br />
The <strong>Hack</strong>ademy DMP -116/209- SYSDREAM