码迷,mamicode.com
首页 > 其他好文 > 详细

CGI编程学习----查询2000W开房数据

时间:2014-12-11 10:10:10      阅读:372      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

原文:CGI编程学习----查询2000W开房数据

0x01:什么是CGI编程?

CGI:Common Gateway Interface

CGI代表Common Gateway Interface(通用网关界面),它使在网络服务器下运行外部分应用程序(或网关)成为可能。

CGI-BIN 目录是存放CGI脚本的地方。

这些脚本使WWW服务器和浏览器能运行外部程序,而无需启动另一个原因程序。

它是运行在Web服务器上的一个程序,并由来自于浏览者的输人触发。CGI是在HTTP服务器下运行外部程序(或网关)的一个接口,它能让网络用户访问远程系统上的使用类型程序,就好像他们在实际使用那些远程计算机一样。 CGI能够让浏览者与服务器进行交互,如果你曾经遇到过在网络上填表或者进行搜索,就很有可能就是用的CGI。

 

0x02:用什么语言可以编写CGI编程?

CGI应用程序可以由大多数的编程语言编写,如Perl(Practical Extraction and Report Language)、C\C++、Java 和Visual Basic等。不过对于那些没有太多编程经验的网页制作人来说,实在是一个不小的难题(测试CGI程序需要html测试文件)。

 

0x03:CGI应用程序的工作原理

1.浏览器通过HTML表单或超链接请求指上一个CGI应用程序的URL。

2.服务器收发到请求。

3.服务器执行指定所CGI应用程序。

4.CGI应用程序执行所需要的操作,通常是基于浏览者输人的内容。

5.CGI应用程序把结果格式化为网络服务器和浏览器能够理解的文档(通常是HTML网页)。

6.网络服务器把结果返回到浏览器中。

 

0x04:CGI程序实现

C语言实现代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 char from_hex(char ch)
 6 {
 7     return isdigit(ch) ? ch - 0 : tolower(ch) - a + 10;
 8 }
 9 
10 char * gb2312_to_chinese(char *gb2312)//将gb2312编码转换为汉字
11 {
12     if (strstr(gb2312, "%"))
13     {
14         char *buf = (char *)malloc(strlen(gb2312) + 1);
15         char *pstr = gb2312;
16         char *pbuf = buf;
17         while (*pstr)
18         {
19             if (*pstr == %)
20             {
21                 if (pstr[1] && pstr[2])
22                 {
23                     *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
24                     pstr += 2;
25                 }
26             }
27             else if (*pstr == +)
28             {
29                 *pbuf++ =  ;
30             }
31             else
32             {
33                 *pbuf++ = *pstr;
34             }
35             pstr++;
36         }
37         *pbuf = \0;
38         return buf;
39     }
40 }
41 
42 void main()
43 {
44     printf("Content-type:text/html \n\n");//HTML语言
45     char searchstr[256] = "";
46     gets(searchstr);//从浏览器获得输入
47     char *p = strchr(searchstr, &);//处理从浏览器获取的输入,去掉多余的部分 searchstr[256] = "我们的输入&%ds%ads%fadsa8d8hd"
48     if (p != NULL)
49     {
50         *p = \0;
51     }
52     char *pstart = searchstr + 10;
53     pstart = gb2312_to_chinese(pstart);//从浏览器获取到的输入是gb2312编码方式 需要转换成汉字 才能进行查找
54     int j = 0;
55     char pathr[200] = "kaifang.txt";
56     FILE *pfr = fopen(pathr, "r");//打开开房数据
57     if (pfr == NULL)
58     {
59         puts("打开失败");
60         return;
61     }
62     else
63     {
64         while (!feof(pfr))
65         {
66             char buffer[256] = { 0 };
67             fgets(buffer, 256, pfr);
68             char *p = strstr(buffer, pstart);
69             if (p != NULL)
70             {
71                 j++;//计数器
72                 puts(buffer);
73                 puts("<br><br>");//换行
74             }
75         }
76         puts("<br><br>");//换行
77         printf("找完了,找到 %d 个 %s ", j, pstart);
78         puts("<br><br>");//换行
79         puts("<br><br>");//换行
80     }
81 }

测试CGI程序所用的html文件:

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
 5 <title>酒店开房记录查询</title>
 6 <style>
 7     a:visited{color:#0000FF;}
 8     #div{width:600px;margin:0 auto};
 9 </style>
10 </head>
11 <body>
12 <div id="div">
13 <table style="line-height:1px;">
14     <tr>
15         <td>
16             <p align="center"><img src="logo.gif" width="270" height="129" usemap="#mp"/></p>
17         </td>
18     </tr>
19     <tr>
20     <td>
21     <p align="center">
22 <form id = "form"  name = "form" method = "post" action = "http://127.0.0.1/cgi-bin/xxoo.cgi">
23     <input type="text" name="searchstr" style="width:400px;height:30px;" placeholder="请输入你的那个他(她),看看有没有出轨"/>
24     <input type="submit" name="sousuoanniu" value="猥琐一下" style="width:95px;height:35px;" />
25 </p>
26     </td>
27     </tr>
28         <tr>
29  <tr>
30  <tr>
31  <tr>
32         <td>
33             <p align="center">
34             <font size="2">酒店开放记录查询 已经总共2000万用户信息泄漏</font>
35 
36             </p>
37             <br>
38             <br>
39  <br>
40  <br>
41  <br>
42  <br>
43  <br>
44             <p align="center">
45             <font  size="2">
46                 友情提醒:数据来源于网络仅供参考,使用时请遵守当地法律法规
47                 
48             </font>
49             </p>
50         </td>
51     </tr>
52 </table>  
53 </div>
54 </body>
55 </html>

0x05:测试效果

环境:Apache2.2,WIN8.1

bubuko.com,布布扣

找到Apache的安装目录将写好的CGI程序生成为.exe可执行程序,将后缀改为.cgi然后放在Apache 2.2\cgi-bin目录下

将测试CGI程序所用的html文件放在Apache 2.2\htdocs目录下

 bubuko.com,布布扣

效果:

bubuko.com,布布扣

bubuko.com,布布扣

 

bubuko.com,布布扣

 

0x06:注意

printf("Content-type:text/html \n\n");//HTML语言
此行通过标准输出将字符串″Contenttype:text/plain/n/n″传送给Web服务器。它是一个MIME头信息,它告诉Web服务器随 后的输出是以纯ASCII文本的形式。请注意在这个头信息中有两个换行符,这是因为Web服务器需要在实际的文本信息开始之前先看见一个空行。

 

CGI编程学习----查询2000W开房数据

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://www.cnblogs.com/lonelyxmas/p/4156909.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!