用标准C语言实现cgi通讯录
时间: 2024-01-21 09:13:18 浏览: 166
好的,下面是一个基于标准C语言实现的简单CGI通讯录示例:
1. 设计数据库:您可以使用SQLite或其他数据库来存储通讯录信息。以下是一个使用SQLite的示例:
```c
#include <sqlite3.h>
int main() {
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("contacts.db", &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
// create contacts table
rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS contacts ("
"id INTEGER PRIMARY KEY,"
"name TEXT,"
"phone TEXT,"
"email TEXT"
");", 0, 0, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
return 0;
}
```
2. 编写CGI脚本:以下是一个基于标准C语言实现的CGI脚本示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
int main() {
char *data;
char *method;
char *name;
char *phone;
char *email;
sqlite3 *db;
char *zErrMsg = 0;
int rc;
printf("Content-Type:text/html;charset=utf-8\n\n");
data = getenv("QUERY_STRING");
method = getenv("REQUEST_METHOD");
if (strcmp(method, "POST") == 0) {
// read form data
scanf("%ms", &data);
name = strstr(data, "name=") + 5;
phone = strstr(data, "phone=") + 6;
email = strstr(data, "email=") + 6;
// remove '+' from phone number
char *p = phone;
while (*p) {
if (*p == '+') {
*p = ' ';
}
p++;
}
// insert into database
rc = sqlite3_open("contacts.db", &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char sql[200];
sprintf(sql, "INSERT INTO contacts (name, phone, email) VALUES ('%s', '%s', '%s');", name, phone, email);
rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
printf("<p>Contact added: %s, %s, %s</p>\n", name, phone, email);
} else {
// display contacts
rc = sqlite3_open("contacts.db", &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
printf("<table><tr><th>Name</th><th>Phone</th><th>Email</th></tr>\n");
rc = sqlite3_exec(db, "SELECT * FROM contacts;", callback, 0, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
printf("</table>\n");
printf("<form method=\"POST\" action=\"\">\n");
printf("<p>Name: <input type=\"text\" name=\"name\"></p>\n");
printf("<p>Phone: <input type=\"text\" name=\"phone\"></p>\n");
printf("<p>Email: <input type=\"email\" name=\"email\"></p>\n");
printf("<p><input type=\"submit\" value=\"Add Contact\"></p>\n");
printf("</form>\n");
}
free(data);
return 0;
}
int callback(void *NotUsed, int argc, char **argv, char **azColName) {
for (int i = 0; i < argc; i++) {
printf("<td>%s</td>", argv[i] ? argv[i] : "NULL");
}
printf("</tr>\n");
return 0;
}
```
3. 设计网页:以下是一个使用HTML和CSS设计的简单通讯录网页示例:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Contacts</title>
<style>
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 5px;
}
</style>
</head>
<body>
<h1>Contacts</h1>
<hr>
<?php include "contacts.cgi" ?>
</body>
</html>
```
希望这些示例能够帮助到您。请注意,这只是一个简单的示例,您可能需要根据您的需求进行更改。
阅读全文