同样是POST模式下,开头声明的printf格式Context-Type与Content-Type却有很大不同.
Context-Type: printf("Context-Type:text/html;charset=UTF-8\n\n"); -----网页不识别html格式语言,都原样输出在网页上.如果cgi里面没有html格式语言,只是做其他处理,对结果没有影响.
Content-Type: printf("Content-Type:text/html;charset=UTF-8\n\n"); ----网页会识别html语言
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sqlite3.h"
#define SQL_SELECT "select name, devid, devname from userinfo where username='sume';"
struct __userinfo{
char name[20];
char dearname[20];
char telephone[12];
char address[200];
char devid[26];
char devname[20];
}user;
int main(void)
{
//声明网页格式
//printf("Content-Type:text/html;charset=UTF-8\n\n");
printf("Context-Type:text/html;charset=UTF-8\n\n");
//获取网页响应
int len = atoi(getenv("CONTENT_LENGTH"));
char buf[100]={0};
int ret = fread(buf, 1, 256, stdin);
//打开SQL
sqlite3 *ppdb = NULL;
ret = sqlite3_open("/mnt/hgfs/Share/program3/userinformation.db", &ppdb);
if(ret != SQLITE_OK)
{
printf("<p>SQLITE OPEN FAIL\n\n");
return -1;
}
char *err = NULL;
char **result = NULL;
//显示本人设备信息内容
int row = 0, col = 0;
result = NULL;
ret = sqlite3_get_table(ppdb, SQL_SELECT, &result, &row, &col, &err);
if(ret != SQLITE_OK)
{
printf("SEE FAIL : %s\n", err);
sqlite3_close(ppdb);
printf("<body>\n\n");
printf("<meta http-equiv=\"content-Type\"content=\"text/html;charset=UTF-8\">\n\n");
printf("<h2 style=\"text-align:center\"> 服务器繁忙,请稍后 </h2>\n\n");
printf("<center><input type=\"button\" value=\"返回\" onclick=\"window.location.href='http://192.168.14.200/M_userface.html'\">\n\n");
printf("<input type=\"button\" value=\"退出\" onclick=\"window.location.href='http://192.168.14.200/M_login.html'\"></center>\n\n");
printf("</body>\n\n");
return -1;
}
int i=0, j=0;
for(i=1; i<(row+1); i++)
{
for(j=0; j<col; j++)
{
//printf("%s ", result[i*col+j]);
//存储表格原信息
switch(j)
{
case 0: strcpy(user.name, result[i*col+j]);break;
case 1: strcpy(user.devid, result[i*col+j]);break;
case 2: strcpy(user.devname, result[i*col+j]);break;
}
}
//printf("\n\n");
}
printf("<br> </br>");
printf("<br> </br>");
printf("<br> </br>");
printf("<br> </br>");
printf("<center><table border=\"1\" cellpadding=\"10\" cellspacing=\"0\"></center>");
printf("<tr>");
printf(" <th>姓名</th>");
printf(" <th>设备名称</th>");
printf(" <th>设备ID</th>");
printf("</tr>");
printf("<tr>");
printf(" <td>%s</td>", user.name);
printf(" <td>%s</td>", user.devname);
printf(" <td>%s</td>", user.devid);
printf("</tr>");
printf("</table>");
//释放查询结果空间
sqlite3_free_table(result);
//关闭SQL
sqlite3_close(ppdb);
//按键 选择跳转其他页面
printf("<body>");
printf("<meta http-equiv=\"content-Type\"content=\"text/html;charset=UTF-8\">\n\n");
printf("<center><table border=\"0\" cellpadding=\"10\" cellspacing=\"0\"></center>");
printf("<tr>");
printf("<td><input type=\"button\" value=\"返回\" onclick=\"window.location.href='http://192.168.14.200/M_userface.html'\"></td>");
printf("<td><input type=\"button\" value=\"退出\" onclick=\"window.location.href='http://192.168.14.200/M_login.html'\"></td>");
printf("</tr>");
printf("</table>");
printf("</body>");
return 0;
}