17.7 How do I…Access and use CGI environment variables?
Problem
I need to retrieve and use CGI environment variables in my application. Information such as the IP address of the remote host making the request is necessary in developing sophisticated applications. How do I access and use CGI environment variables in my web applications? Technique
All CGI environment variables that are part of the Common Gateway Interface specification are passed from the Oracle Web Listener to the PL/SQL Agent. All relevant environment variables are available through PL/SQL. The GET_CGI_ENV function from the OWA_UTIL package enables you to access CGI environment variables. The procedures and functions provided by the OWA_UTIL package are shown in Table 17.8.
Table 17.8 OWA_UTIL package
Procedure Description owa_util.get_cgi_env Returns the value of the specified CGI variable. owa_util.get_owa_service_path Prints the currently active service path and its virtual path. owa_util.print_cgi_env Prints all CGI variables. owa_util.signature Enables the developer to create a signature line for the document. owa_util.showpage Allows the user to display the output from a procedure. owa_util.showsource Prints the source of the specified PL/SQL module. owa_util.mime_header Changes the default MIME header. owa_util.http_header_close Outputs two newlines to close the HTTP header. owa_util.redirect_url Redirects the browser to visit the specified URL. owa_util.status_line Enables the developer to send a standard HTTP status code to the browser. owa_util.bind_variables Prepares a SQL query by binding variables to it and stores the output in an opened cursor. owa_util.who_called_me Returns the name of the calling PL/SQL code unit. owa_util.get_procedure Returns the name of the current procedure. owa_util.choose_date Prints three HTML form elements to select the day, the month, and the year. owa_util.to_date Converts the owa_util.dateType datatype to the standard Oracle database DATE type. owa_util.listprint Prints an HTML select list from the output of a query. owa_util.calendarprint Prints a calendar in HTML. owa_util.cellsprint Prints an HTML table from the output of a query. owa_util.tableprint Prints an Oracle table as a pre-formatted HTML table. The OWA_UTIL.GET_CGI_ENV function returns the value of the CGI variable that is passed in as a parameter.
Steps
1. Run SQL*Plus and connect as the WAITE user account. Run the CHP17_18.SQL in SQL*Plus. The stored procedure contained in the file displays the available CGI environment variables.
SQL> START CHP17_18
SQL> CREATE OR REPLACE PROCEDURE CGI_ENV17 AS
2 X VARCHAR2(80);
3 BEGIN
4 HTP.PRINT(‘
CGI_ENV17 ’);5 HTP.PRINT(‘
CGI Environment Variables
’);6 X := OWA_UTIL.GET_CGI_ENV(‘AUTH_TYPE’);
7 HTP.PRINT(‘AUTH_TYPE = ‘||X||’
’);8 X := OWA_UTIL.GET_CGI_ENV(‘GATEWAY_INTERFACE’);
9 HTP.PRINT(‘GATEWAY_INTERFACE = ‘||X||’
’);10 X := OWA_UTIL.GET_CGI_ENV(‘HTTP_USER_AGENT’);
11 HTP.PRINT(‘HTTP_USER_AGENT = ‘||X||’
’);12 X := OWA_UTIL.GET_CGI_ENV(‘PATH_INFO’);
13 HTP.PRINT(‘PATH_INFO = ‘||X||’
’);14 X := OWA_UTIL.GET_CGI_ENV(‘PATH_TRANSLATED’);
15 HTP.PRINT(‘PATH_TRANSLATED = ‘||X||’
’);16 X := OWA_UTIL.GET_CGI_ENV(‘REMOTE_HOST’);
17 HTP.PRINT(‘REMOTE_HOST = ‘||X||’
’);18 X := OWA_UTIL.GET_CGI_ENV(‘REMOTE_ADDR’);
19 HTP.PRINT(‘REMOTE_ADDR = ‘||X||’
’);20 X := OWA_UTIL.GET_CGI_ENV(‘REMOTE_USER’);
21 HTP.PRINT(‘REMOTE_USER = ‘||X||’
’);22 X := OWA_UTIL.GET_CGI_ENV(‘REMOTE_IDENT’);
23 HTP.PRINT(‘REMOTE_IDENT = ‘||X||’
’);24 X := OWA_UTIL.GET_CGI_ENV(‘SERVER_PROTOCOL’);
25 HTP.PRINT(‘SERVER_PROTOCOL = ‘||X||’
’);26 X := OWA_UTIL.GET_CGI_ENV(‘SERVER_SOFTWARE’);
27 HTP.PRINT(‘SERVER_SOFTWARE = ‘||X||’
’);28 X := OWA_UTIL.GET_CGI_ENV(‘SERVER_NAME’);
29 HTP.PRINT(‘SERVER_NAME = ‘||X||’
’);30 X := OWA_UTIL.GET_CGI_ENV(‘SERVER_PORT’);
31 HTP.PRINT(‘SERVER_PORT = ‘||X||’
’);32 X := OWA_UTIL.GET_CGI_ENV(‘SCRIPT_NAME’);
33 HTP.PRINT(‘SCRIPT_NAME = ‘||X||’
’);34 END;
35 /
Procedure created.
Line 1 specifies the keywords required to create the stored procedure. Line 2 declares a variable used to hold the CGI variables as they are returned to the PL/SQL code. Lines 3 through 34 contain the procedure body. Line 6 is representative of most lines in the stored procedure. The OWA_UTIL. GET_CGI_ENV function is used to return the value of a CGI environment. The HTP.PRINT procedure is used to print a line to the browser.
When the page is displayed, the CGI variables are retrieved from the server by the OWA_UTIL procedures and displayed within the document.
2. Load your browser and browse the URL created by the stored procedure. Figure 17.38 shows the results of the operation.
How It Works
The GET_CGI_ENV function from the OWA_UTIL package returns the value of the specified CGI variable to the PL/SQL function. The PRINT procedure in the HTP package is used to print text or HTML commands to the browser. Step 1 creates a stored procedure using the OWA_UTIL.GET_CGI_ENV to retrieve the values of each available CGI environment variable. Step 2 displays the page within a browser.
Comments
CGI variables provide you with information required to create complex web documents and applications. The OWA_UTIL package gives you easy access to these variables for use within your applications. Other OWA_UTIL procedures enable you to control the environment and display debugging information.