COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button
C++ LANGUAGE CGI ENVIRONMENT VARIABLE
CGI Environment Variables
All the CGI program will have access to the following environment variables. These variables play an important role while writing any CGI program.
Variable Name
Description
CONTENT_TYPE The data type of the content, used when the client is sending attached content to the server. For example file upload etc.
CONTENT_LENGTH The length of the query information that is available only for POST requests.
HTTP_COOKIE Returns the set cookies in the form of key & value pair.
HTTP_USER_AGENT The User-Agent request-header field contains information about the user agent originating the request. It is a name of the web browser.
PATH_INFO The path for the CGI script.
QUERY_STRING The URL-encoded information that is sent with GET method request.
REMOTE_ADDR The IP address of the remote host making the request. This can be useful for logging or for authentication purpose.
REMOTE_HOST The fully qualified name of the host making the request. If this information is not available then REMOTE_ADDR can be used to get IR address.
REQUEST_METHOD The method used to make the request. The most common methods are GET and POST.
SCRIPT_FILENAME The full path to the CGI script.
SCRIPT_NAME The name of the CGI script.
SERVER_NAME The server's hostname or IP Address.
SERVER_SOFTWARE The name and version of the software the server is running.
Here is small CGI program to list out all the CGI variables.
#include <iostream>
#include <stdlib.h>
using namespace std;
const string ENV[ 24 ] = {
"COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE",
"HTTP_ACCEPT", "HTTP_ACCEPT_ENCODING",
"HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION",
"HTTP_HOST", "HTTP_USER_AGENT", "PATH",
"QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT",
"REQUEST_METHOD", "REQUEST_URI", "SCRIPT_FILENAME",
"SCRIPT_NAME", "SERVER_ADDR", "SERVER_ADMIN",
"SERVER_NAME","SERVER_PORT","SERVER_PROTOCOL",
"SERVER_SIGNATURE","SERVER_SOFTWARE" };
int main ()
{
cout << "Content-type:text/html\r\n\r\n";
cout << "<html>\n";
cout << "<head>\n";
cout << "<title>CGI Environment Variables</title>\n";
cout << "</head>\n";
cout << "<body>\n";
cout << "<table border = \"0\" cellspacing = \"2\">";
for ( int i = 0; i < 24; i++ )
{
cout << "<tr><td>" << ENV[ i ] << "</td><td>";
// attempt to retrieve value of environment variable
char *value = getenv( ENV[ i ].c_str() );
if ( value != 0 ){
cout << value;
}else{
cout << "Environment variable does not exist.";
}
cout << "</td></tr>\n";
}
cout << "</table><\n";
cout << "</body>\n";
cout << "</html>\n";
return 0;
}
The output is as follows:
COMSPEC Environment variable does not exist.
DOCUMENT_ROOT /var/www/onlionedu.blogspot
GATEWAY_INTERFACE CGI/1.1
HTTP_ACCEPT text/html, application/xhtml+xml, */*
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE en-US
HTTP_CONNECTION Keep-Alive
HTTP_HOST www.onlionedu.blogspot.com
HTTP_USER_AGENT Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
PATH /sbin:/usr/sbin:/bin:/usr/bin
QUERY_STRING
REMOTE_ADDR 183.82.104.71
REMOTE_PORT 50902
REQUEST_METHOD GET
REQUEST_URI /cgi-bin/cpp_env.cgi
SCRIPT_FILENAME /var/www/cgi-bin/cpp_env.cgi
SCRIPT_NAME /cgi-bin/cpp_env.cgi
SERVER_ADDR 66.135.33.172
SERVER_ADMIN webmaster@onlionedu.blogspot.com
SERVER_NAME www.onlionedu.blogspot.com
SERVER_PORT 80
SERVER_PROTOCOL HTTP/1.1
SERVER_SIGNATURE
SERVER_SOFTWARE Apache
<
No comments:
Post a Comment