Write CGI programs with VC ++

zhaozj2021-02-11  183

In recent years, Internet and intranet have developed rapidly, and companies have built their own intranet to strengthen the links between the enterprises, promote communication with the external world, and enhance the competitiveness of the enterprise. By building your own Internet server, companies can publish advertisements on the Internet, introducing the company's situation and products, collect customer opinions, and establish virtual electronic shopping malls. Everyone knows that writing an HTML hypertext file is on the World Wide Web server, you can access your homepage through a variety of browsers, but how to make your static home page to respond to customer action and contact your local database, The corresponding content of the database is transmitted to the remote browser? This needs to be contacted through the CGI interface.

The CGI full name is Common Gateway Interface, which is the interface of the WWW server calls the external program through the CGI, the WW W server can do some work in some itself.

The CGI interface is also different under different operating systems, which can be divided into a Standard CGI interface, the DOS CGI interface and the WIN CG I interface, etc. The Standard CGI interface is used for UNIX systems, which enables contact between WWW Server and CGI applications through environment variables. The DOS CGI interface is the same as the STANDARD CGI interface principle. The WIN CGI interface is different from the top. Since more and more WWW Server uses a Windows system, the Windows system does not effectively deliver environment variables, and a Windows CGI standard is proposed by Bob Denny and is constantly promoted.

WIN CGI is linked through .ini file. In a Windows environment, many applications use the .ini file, the profile file to define some basic settings, and between the WIN CGI program and WWW Server are also connected through the Pro File file. When the WWW Client triggers the CGI program via WWW Server, WWW Server actively generates several files in the temporary directory, divided into .ini, .inp, .out and other files. When the WWW Server generates a CGI program, pass the absolute path of the .ini file to the CGI process through the command line parameter. The .ini file also contains .inp, .out and other file path information. This way, the CGI program can read the information provided by the .ini and .inp file, understand all the data from the WWW Server end and the WWW Client side. Then, the CGI program can call an external program or perform the functionality of the external program directly by the CGI, and write information to the client to the HTML text in the form .out file. Finally, WWW Server takes the HTML text of the .out file, transfer the information to the WWW Client side and deletes the files in the temporary directory. So, what data does the CGI's profile file contain? Like Windows Other .ini files, the CGI profile file contains many sessions, each session contains some Key, the following is some of the CGI PROFILE

[CGI] Request method = post // Transfer mode, the form method in HTML has GET, POST, HEAD, etc. Server Software = Website / 1.1e // Server name and version. Server name = 199.199.198.55 // Server IP address. Server port = 80 // Socket Port for Server. Server admin=tomy@vico.bme.zju.edu.cn/server administrator's E-mail address. Referr = http: //199.199.198.55///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////wwpread. Remote address = 199.199.198.89 // The IP address of the Client end. [System] Output file = c: /website/cgi-temp/198ws.out/ / The path name of the output file. Content file = c: /website/cgi-temp/198ws.inp/ The path name is entered. [Form Literal] type = b // The received Form Data is the result of the result of the Client side action.

In principle, you can use the language of the CGI program: Perl, Applescript, UNIX Shell, VB, C / C , etc. But in the Windows (NT, 3X, 95) environment, you must also write a delicate WIN CGI program with a mature Windows programming language. The C language is a language that is very popular with programmers, especially VCI as a target-oriented language, has many basic functions of the Windows environment, such as OLE, ODBC, etc. With ODBC, you can easily connect a variety of databases such as FoxPro, Access, and DBASE, without considering the differences in various database structures. The following is an example of a WIN CGI connection database using a WIN CGI connection database. Users can choose on the screen to query the ISP service provider's Internet service price list. The system environment uses Windows 95, the database uses FoxP RO2.5, and WWW Server uses WebSite. 1. Establishing a database This example The CGI program is connected to the database through the ODBC, so after the database is built, apply 32-bit ODBC drivers to register. 2. Write an HTML program user to select a service type on the screen, press the "Submit" button to query each price column of this service type. Its HTML text is described below: {title> Wikko Internet Service Charges

 

Wikco Internet Service Charges
The type you want to query is:
  • class category
  • Class B user
  • Class C user < Li> D user

    3. Writing VC CGI Program Website provides a CCGI class for C programmers, which can be found in the / Website directory. This class provides some convenient functions that do not require programmers to write some underlying work directly to the CGI interface, fully reflecting the advantages of object-oriented language. A "Dialog Based" project framework is created in VC 4.0. The CCGI class is introduced into the frame, which is about CGI.H, CGI .cpp joins the program. The CUSERSET class corresponding to the database is then created with the VC ClassWizard tool, which inherits the CRecordset class in the MFC class library. The main code is added in 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 IF with command line parameters (cgi.calledascgi ()) {Try {processcgi (& cgi); // Do all true work here} catch (CEXCEPTION * E) {cgi.handleException (e); // Abnormal handling E-> delete ();}} return false;} void processcgi (cgi * cgi) {Int i; cstring straTemp, strWebmaster, strrefer; strWebmaster = CGI-> getcgivar ("server admin"); // gets the E-mail address of the Server administrator. Strreferr = cgi-> getcgivar ("referer"); // Get the URL path of the home page. CStringArray Csafields; CGI-> EnumformfieldNames (csafields); // Get all variable names, placed in an array. CGI-> WritehtmlHeader (); cgi-> writehtmlbody (" VC CGI Test </ Title> </ head> / 015/012"); cgi-> writehtmlbody ("<body> / 015 / 012 "); cstring csafieldval; csafieldval = cgi-> getformfield (csafields [0]); // Get the value of the first variable, ie the value of the" Usertype "variable. CGI-> WritehtmlBody ("<H1> Internet Service Charges Query Results </ H1> <HR>"); CUSERSET M-USER; / / Installation CUSERSET Class CHAR SFILTER [80]; WSPrintf (Sfilter, "User-Type = '% s' '', csafieldval; m-user.m-strfilter = sfilter; // Setting filter. M-user.Open (); strTemp = "<h2> m-user.m-user -Type "Class User" "</ h2> / 015/012"; CGI-> WritehtmlBody (strTemp); strTemp = "<p> enter network fee:" m-user-fare-ini ". </ P> / 015/012 "; CGI-> WritehtmlBody (strTemp); ... strTemp =" <p> communication fee: " m-user.m-value-com ". </ P> / 015/012 "; CGI- > WritehtmlBody (strTemp); m-user.close (); if (! Strreferr.Isempty ()) // Set back to the home page anchor point.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-4043.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="4043" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.028</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'GL3PcHbWZT4fPL4Tm9jdy5y2vo1ld93jGzP8HDZoyaQELC9MDOCNcLdypVctGCeQ_2F97F_2BsVNAZ_2BeF8pkkIc0AA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>