In recent years, with the rapid development of Internet and Intranet, enterprises have established their own intranets to enhance their internal connections, promote communication with the outside world, and enhance their competitiveness. By establishing your own internet server, enterprises can publish advertisements on the internet, introduce the company's situation and products, Collect customer opinions, and establish virtual electronic malls. We all know that writing an HTML hypertext file on the World Wide Web server allows you to access your home page through various browsers, but how can you make your static home page respond to customer actions, and contact the local database to transfer the corresponding content of the database to the remote browser? You need to contact us through the CGI interface.
The full name of CGI is the Common Gateway Interface, which is the interface through which the WWW server calls external programs. Through CGI, The ww w server can do some work that is beyond its own capabilities.
CGI interfaces vary with operating systems, including Standard CGI interfaces, dos cgi interfaces, and win cg I interfaces. The Standard CGI interface is used in Unix systems. It uses environment variables to realize the connection between WWW server and CGI applications. The principle of dos cgi interface is similar to that of Standard CGI interface. The win cgi interface is very different from the previous two interfaces. Because more and more www servers use Windows systems, and Windows systems cannot effectively transmit environment variables, Bob Denny proposed a Windows CGI standard which can be continuously promoted.
Win cgi is connected through the. ini file. In Windows, many applications are used. the INI file, that is, the profile file, defines some basic settings, and the connection between the win cgi program and the WWW server is also achieved through the pro file. When the WWW client triggers the CGI program through the WWW server, the WWW server first dynamically generates several files under the temporary directory, including. ini,. Indium,. Out and other files. When the WWW server generates the CGI program process, it uses the command line parameter to pass the absolute path of the. ini file to the CGI process .. The INI file also contains file path information such as. Indium and. ou t. In this way, the CGI program can read the information provided by the. ini and. Indium files to understand all the data from the WWW server and WWW client. Then, the CGI program can call an external program or execute the functions of the external program directly by CGI, and write the information to be sent to the client in the form of HTML text on the. Out file. Finally, the WWW server obtains the html text of the. out file, transmits the information to the WWW client, and deletes the files under the temporary directory. So what data does the CGI profile file contain? Similar to other windows. ini files, the CGI profile file contains many sessions, and each session contains some keys. The following is part of a CGI profile.
[CGI]
Request Method = post // The transfer method, that is, form methods in HTML include get, post, Head, and so on.
Server Software = website/1.1e
// Server name and version.
Server name = 199.199.198.55
// The IP address of the server.
Server Port = 80 // socket port of the server.
Server Admin = the e-mail address of the tomy@vico.bme.zju.edu.cn // Server Manager.
Referer = http: // 199.199.198.55/
// URL of the home page.
Remote address = 199.199.198.89
// The IP address of the client.
[System]
Output file = C:/website/cgi-temp/198ws. Out // path name of the output file.
Content File = C:/website/cgi-temp/198ws. Indium // path of the input file.
[Form literal]
Type = B // The received form data is the result of the client action.
In principle, CGI programs can be written in the following languages: Perl, applescript, Unix shell, VB, and C/C ++. However, in Windows (NT, 3x, and 95) environments, sophisticated windows programming languages must be used to compile sophisticated windows CGI programs. C language is a popular language for programmers, especially VCI ++, as an object-oriented language. It has many basic functions in the Windows environment, such as Ole and ODBC. Using ODBC, you can easily connect to multiple databases, such as FoxPro, access, and DBASE without considering the differences in database structures.
The following is an example of connecting to a database using win cgi written in VC ++ 4.0. You can select on the screen to query the internet service price list provided by the ISP service provider. The system environment uses Windows 95, the database uses Foxp ro2.5, And the WWW server uses website.
1. Create a database
In this example, the CGI program connects to the database through ODBC. Therefore, after the database is created, the 32-bit ODBC driver is used to register the program.
2. Compile HTML programs
You can select a service type on the screen and press the "Submit" key to query the price columns of this service type. Its HTML text is described as follows:
<HTML>
<Head> {Title> Internet service billing standard of Vico </title> <PRE> </P>
<HR> <form method = "Post" Action = "CGI-Win/wincgi.exe">
The types you want to query are:
<Ul> <li> Class A users <input name = "usertype" type = radio value = "A">
<Li> Class B users <input name = "usertype" type = radio value = "B">
<Li> Class C users <input name = "usertype" type = radio value = "C">
<Li> Class D users <input name = "usertype" type = radio value = "D">
</Ul>
<Center>
<Input type = submit value = "Submit"> <input type = reset value = "cancel"> </center>
</PRE> </form> <HR>
</Html>
3. Compile the VC ++ CGI program
Website provides a ccgi class for C ++ programmers, which can be found under the/website directory. This class provides some convenient functions, without the need for programmers to write some underlying work that deals directly with the CGI interface, fully reflecting the advantages of object-oriented language. Create a "dialog based" Engineering Framework in VC ++ 4.0. Introduce the ccgi class into the framework to add CGI. h and CGI. cpp to the program. Then, use the classwizard tool of VC ++ to create a cuserset class corresponding to the database, which inherits the crecordset class in the MFC class library.
The main code is added to the wincgi. cpp file.
// Program: wincgi. cpp
# Include "stdafx. H"
# Include "wincgi. H"
# Include "CGI. H"
# Include "userset. H"
......
Void processcgi (ccgi * CGI );
Bool cbaseapp: initinstance ()
{
Ccgi CGI (m-ipcmdline); // instantiate the ccgi class and initialize it with command line parameters
If (CGI. calledascgi ())
{
Try
{
Processcgi (& CGI );
// Do all the real work here
}
Catch (cexception * E)
{
CGI. handleexception (E );
// Exception Handling
E-> Delete ();
}
}
Return false;
}
Void processcgi (ccgi * CGI)
{
Int I;
Cstring strtemp, strwebmaster, strreferer;
Strwebmaster = CGI-> getcgivar ("server admin ");
// Obtain the email address of the server administrator.
Strreferer = CGI-> getcgivar ("Referer"); // obtain the URL of the home page.
Cstringarray csafields;
CGI-> enumformfieldnames (csafields );
// Obtain all variable names and place them in the array.
CGI-> writehtmlheader ();
CGI-> writehtmlbody ("CGI-> writehtmlbody ("<body>/015/012 ");
Cstring csafieldval;
Csafieldval = CGI-> getformfield (csafields [0]);
// Obtain the value of the first variable, that is, the value of the "usertype" variable.
CGI-> writehtmlbody ("Cuserset M-user;
// Instantiate the cuserset class
Char sfilter [80];
Wsprintf (sfilter, "user-type = '% s'', csafieldval );
M-user.m-strFilter = sfilter;
// Set the filter.
M-user.Open ();
Strtemp = "<H2>" + m-user.m-USER-TYPE + "class user" + "</H2>/015/012 ";
CGI-> writehtmlbody (strtemp );
Strtemp = "<p> Network Access Fee:" + M-user-FARE-INI + ". </P>/015/012 ";
CGI-> writehtmlbody (strtemp );
......
Strtemp = "<p> communication fee:" + m-user.m-FARE-COM + ". </P>/015/012"
;
CGI-> writehtmlbody (strtemp );
M-user.Close ();
If (! Strreferer. isempty ())
// Set the anchor to return to the home page.
{
CGI-> writehtmlbody ("<p> go <a href = /"");
CGI-> writehtmlbody (strreferer );
CGI-> writehtmlbody ("/"> back </a> where you came from. </P>/015/012 ");
}
If (! Strwebmaster. isempty ())
// Set the email address of the server administrator.
{
CGI-> writehtmlbody ("<p> Please email comments ");
CGI-> writehtmlbody ("<a href =/" mailto :");
CGI-> writehtmlbody (strwebmaster );
CGI-> writehtmlbody ("/"> ");
CGI-> writehtmlbody (strwebmaster );
CGI-> writehtmlbody ("</a>. </P>/015/012 ");
}
CGI-> writehtmlbody ("</body>/015/012 ");
CGI-> writehtmlfooter ();
}
4. Conclusion
Compile the program, copy wincgi.exe to the website/cgi-Win/directory, and use the browser to query the results. At this point, the whole process of compiling win cgi program with VC ++ is briefly described. c programmers can also write CGI programs in languages they are familiar. With the powerful functions of VC ++, your WWW server will be omnipotent.