c语言实现CGI通讯录
时间: 2023-07-13 07:08:03 浏览: 214
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即可查询联系人信息,以此类推。
阅读全文