C语言进行站点开发之cgi


  1. 安装Apach

  2. 配置ApacheRuntime

以下的过程中一直点击next

配置CGI,放开配置:AddHandler cgi-script .cgi

2.加入Option,截图

3.编写CGI代码例如以下:

#define _CRT_SECURE_NO_WARNINGS  //取消安全检查

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

 

void main()

{

    //假设想实如今html中也显示。须要加上以下两句

    printf("Content-type:text/html ");

    //通过以下的方式实现查询环境变量的字符串

    printf("%s<br/><br/>", getenv("QUERY_STRING"));

    char szPost[256] = { 0 };

    //获取输入

    gets(szPost);

    //获取输入

    printf("%s<br/><br/>", szPost);

    //这一句是将指针移动到等号位置

    char *p = szPost + 8;

    char *p1 = strchr(szPost,"&");

    *p1 = '';

 

    char cmd[256] = { 0 };

    //字符串映射

    sprintf(cmd, "%s>1.txt", p);

    system(cmd);

    FILE *pf = fopen("1.txt", "r");

    //假设没有到文件末尾就继续

    while (!feof(pf))

    {

        char ch = fgetc(pf);

        if (ch == ' ')

        {

            //换行

            printf("<br/><br/>");

        }

        else

        {

            //打印字符

            putchar(ch);

        }

    }

}

4.点击:本地Windows调试器

在文件资源管理器中打开文件,截图例如以下:

Debug文件夹例如以下:

5.system.exe复制到Apach中的cgi-bin,system.exe改动成system.cgi

6.重新启动Apacheserver,右击ApachàOpen Apache Monitor.弹出例如以下界面:

7.编写下面html

<html>

   <form method="post"action="http://localhost/cgi-bin/system.cgi">

             <p>

                       <input type="text"id="command" name="command"

                                     value="tasklist"action=""/>

                   </p>

                   <p>

                       <input type="submit"name="submit" id="submit" value="提交"/>

                   </p>

         </form>

</html>

 

要注意的是假设:发现网页中仍然输出有错误,这时候可能不是程序的问题。而是缓存的问题,这时候应该关闭网页。让后又一次打开。这时候就能够了。

原文地址:https://www.cnblogs.com/tlnshuju/p/6855233.html