C语言cgi程序在apache上的实现

本文介绍使用apache实现C语言写的cgi程序。
必要条件,安装apache。
首先建立C程序,这里就不多介绍。参照前面的文章或者其它的参考书籍。
建立文件hello.c,内容如下:

#include <stdio.h>
int main()
{
printf(“Content-type:text/html\n\n”);
printf(“<html>”);
printf(“<head><title>welcome to c cgi.</title></head><body>”);
printf(“你好:世界<br/>”);
printf(“</body></html>”);
}

编绎hello.c,生成hello.exe。把hello.exe文件拷到 apache安装/cgi-bin/ 目录下。
然后配置 apache 配置文件 httpd.conf。在配制文件中找到
AddHandler cgi-script .cgi 在这一行后面加上 .exe,并且去掉前边的#
最后在浏览器中输入 http://localhost/cgi-bin/hello.exe,回车。
在浏览器中将显示:你好:世界。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
CGI(Common Gateway Interface)是一种常见的Web编程技术,可以让Web服务器调用外部程序实现动态网页。本文将介绍如何使用C语言实现一个简单的通讯录管理系统。 首先,需要在Web服务器上配置好CGI环境。这里以Apache服务器为例,可以在httpd.conf文件中添加如下配置: ``` ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/" <Directory "/usr/local/apache2/cgi-bin"> AllowOverride None Options None Require all granted </Directory> ``` 这样,可以将CGI程序存放在/usr/local/apache2/cgi-bin目录下,并在Web页面中通过访问http://localhost/cgi-bin/program.cgi的方式调用程序。 接下来,编写C语言代码实现通讯录管理系统。这里使用了文件存储数据,因此需要先创建一个contacts.txt文件存放通讯录信息。 程序主要分为两部分:解析HTTP请求和处理通讯录操作。在解析HTTP请求时,需要读取请求方法、请求路径和请求参数等信息,然后根据请求路径调用相应的处理函数。在处理通讯录操作时,需要读取contacts.txt文件中的数据,并根据请求参数进行增删改查等操作。 以下是示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 1024 // 解析HTTP请求 void parse_request(char *request_method, char *request_path, char *request_params) { char *request_body = getenv("QUERY_STRING"); if (request_body == NULL) { request_method[0] = '\0'; request_path[0] = '\0'; request_params[0] = '\0'; return; } sscanf(request_body, "%[^&]&%[^&]&%s", request_method, request_path, request_params); } // 处理添加联系人操作 void add_contact(char *params) { FILE *fp; char line[MAX_LINE]; fp = fopen("contacts.txt", "a"); if (fp == NULL) { printf("Content-type: text/plain\n\n"); printf("Failed to open file\n"); return; } sprintf(line, "%s\n", params); fputs(line, fp); fclose(fp); printf("Content-type: text/plain\n\n"); printf("Success"); } // 处理删除联系人操作 void delete_contact(char *params) { FILE *fp1, *fp2; char line[MAX_LINE]; int found = 0; fp1 = fopen("contacts.txt", "r"); if (fp1 == NULL) { printf("Content-type: text/plain\n\n"); printf("Failed to open file\n"); return; } fp2 = fopen("temp.txt", "w"); if (fp2 == NULL) { printf("Content-type: text/plain\n\n"); printf("Failed to open file\n"); fclose(fp1); return; } while (fgets(line, MAX_LINE, fp1) != NULL) { if (strstr(line, params) == NULL) { fputs(line, fp2); } else { found = 1; } } fclose(fp1); fclose(fp2); if (found) { remove("contacts.txt"); rename("temp.txt", "contacts.txt"); printf("Content-type: text/plain\n\n"); printf("Success"); } else { remove("temp.txt"); printf("Content-type: text/plain\n\n"); printf("Contact not found"); } } // 处理修改联系人操作 void update_contact(char *params) { FILE *fp1, *fp2; char line[MAX_LINE]; int found = 0; fp1 = fopen("contacts.txt", "r"); if (fp1 == NULL) { printf("Content-type: text/plain\n\n"); printf("Failed to open file\n"); return; } fp2 = fopen("temp.txt", "w"); if (fp2 == NULL) { printf("Content-type: text/plain\n\n"); printf("Failed to open file\n"); fclose(fp1); return; } while (fgets(line, MAX_LINE, fp1) != NULL) { if (strstr(line, params) == NULL) { fputs(line, fp2); } else { found = 1; sprintf(line, "%s\n", params); fputs(line, fp2); } } fclose(fp1); fclose(fp2); if (found) { remove("contacts.txt"); rename("temp.txt", "contacts.txt"); printf("Content-type: text/plain\n\n"); printf("Success"); } else { remove("temp.txt"); printf("Content-type: text/plain\n\n"); printf("Contact not found"); } } // 处理查询联系人操作 void query_contact(char *params) { FILE *fp; char line[MAX_LINE]; int found = 0; fp = fopen("contacts.txt", "r"); if (fp == NULL) { printf("Content-type: text/plain\n\n"); printf("Failed to open file\n"); return; } while (fgets(line, MAX_LINE, fp) != NULL) { if (strstr(line, params) != NULL) { printf("Content-type: text/plain\n\n"); printf("%s", line); found = 1; break; } } fclose(fp); if (!found) { printf("Content-type: text/plain\n\n"); printf("Contact not found"); } } int main() { char request_method[MAX_LINE], request_path[MAX_LINE], request_params[MAX_LINE]; parse_request(request_method, request_path, request_params); // 根据请求路径调用相应的处理函数 if (strcmp(request_path, "/add") == 0) { add_contact(request_params); } else if (strcmp(request_path, "/delete") == 0) { delete_contact(request_params); } else if (strcmp(request_path, "/update") == 0) { update_contact(request_params); } else if (strcmp(request_path, "/query") == 0) { query_contact(request_params); } else { printf("Content-type: text/plain\n\n"); printf("Invalid request"); } return 0; } ``` 编译并将程序命名为program.cgi,然后将其存放在/usr/local/apache2/cgi-bin目录下。此时,在Web页面中访问http://localhost/cgi-bin/program.cgi/add?name=John&phone=123456789即可添加联系人,访问http://localhost/cgi-bin/program.cgi/query?name=John即可查询联系人信息,以此类推。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值