中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > 综合其它
How CGI interacts with C ? zz
作者:未知 时间:2005-09-13 23:33 出处:Blog.ChinaUnix.net 责编:chinaitpower
              摘要:How CGI interacts with C ? zz

adapted from http://www.cs.utk.edu/~cs460.is&r/cgi-c.html

It gives some basics about how to write CGI using C.



CGI applications can be written in any language that can be executed on a Web platform such as: C/C++, Perl, Tcl, Python, Shell Scripts(UNIX), Visual Basic and Applescript. Your choice depends on what you have to do because different languages may be specialized for different purposes. Perl, for instance, is great for string and file manipulation, while C is better for bigger, more complex programs. Perl and C are probably the most used languages for CGI programming.

We all know how CGI interacts with Perl. In Perl, we used $queryString = $ENV to access the environmental variable. In C, you could call the library function "getenv()", which is defined in standard library . Here is a simple example of how to call this function:

Sample source C code (search.c):

#include	<stdio.h>
#include <stdlib.h>

int main(void)
{
char* query;

printf("%s%c%cn","Content-Type:text/html;charset=iso-8859-1",13,10);
printf("<title>Search Result</title>n");
printf("<H3>Search Result</H3>n");

query = getenv("QUERY_STRING");

// Note 1: other functions to truncate the query string

// Note 2: other functions to search for the related documents

// Now, if you find the word and want to display on user's browser, do

printf("n<p> word %s is foundn", query);
}

Note 1: remember the string returned by getenv() is in a format: "key=academic+research&send=search" (assume the user types in "academic research". So you need to do some truncation work to obtain the actual query string.

Note 2: your code to search for the related docs for a specific term.

Then compile the C code as you normally do:

gcc -o search.cgi search.c

Be careful to name your C executable as *.cgi or it will not work on web.

In your form.html:

<FORM ACTION="http://www.cs.utk.edu/~xyz/cgi-bin/search.cgi"
METHOD="GET">
<p> Enter your key word separated by blank:
<INPUT TYPE="text" NAME ="key" SIZE =60 MAXLENGTH=200>
<INPUT TYPE ="Submit" NAME ="send" VALUE="Search">
<INPUT TYPE="Reset" NAME ="clear" VALUE="Reset">
</FORM>

Additional information on this subject could be found in: http://www.hut.fi/u/jkorpela/forms/cgic.html

Also there is a nice cgi-c package which can be found in http://www.boutell.com/cgic/#obtain. They provide more sophisticated library functions than <stdlib.h>.

Written by Kathy Xiayoyan Zhang, April 1999.

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有