linux centos7下 c++编程

在Linux下与在windos下编程没啥区别,可以在windos上实现后,然后更改一些,移植到linux中

yum install gcc
yum install gcc-c++

vi main.cpp    内容如下

#include <iostream>
using namespace std;
int main(){
  cout << "Hello World" << endl;
  return 0;
}
g++ main.cpp            //生成.out  文件
./main.out
或者
g++ main.cpp -o main.out
或者
g++ main.cpp -o main

这样生成的三个文件内容是一样的

 连接mysql

c++需要mysql库(#include<mysql/mysql.h>),需下载mysql-devel

yum install mysql-devel    

 

mysql_config --libs

 vi main.cpp

#include<iostream>             //需加尖括号
#include<mysql/mysql.h>
using namespace std;
void linkToMysql()
{
    MYSQL link;
    mysql_init(&link);
    if(mysql_real_connect(&link,"127.0.0.1","zabbix","zabbix","zabbix",0,NULL,0)==NULL){
        cout<<"连接错误";
    }else{
        cout<<"连接成功";
    }
    mysql_close(&link);
} 
int main(){
linkToMysql();
}

mysql_real_connect()函数原型

MYSQL mysqlrealconnect(
MYSQL mysql,     
const char host,     //ip地址
const char user,     //mysql用户名
const char passwd,    //用户名密码
const char db,      //对应mysql用户可访问数据库名
unsigned int port,     
const char unixsocket, 
unsigned intclientflag)

编译并链接文件

g++ -L/usr/lib64/mysql -lmysqlclient -o main main.cpp
./main    

 cgi编程

让apache支持cgi

1.加载 CGI 支持模块,打开 Apache 配置文件(/etc/httpd/conf),寻找 LoadModule cgi_module modules/mod_cgi.so 解除前面的 #

2.定义 CGI 运行目录,解除在 Apache 配置文件中 ScriptAlias /cgi-bin/ "/var/www/cgi-bin" 前面的 # ( 根据实际情况而定)

3.启用对 CGI 的支持,解除在 Apache 配置文件中 AddHandler cgi-script .cgi 前面的 #

4.修改为如下

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options ExecCGI indexes FollowSymLinks
    Require all granted
    AddHandler cgi-script .cgi
</Directory>

简单测试一下

注意是在 /var/www/cgi-bin  目录

a.cpp

#include <iostream>
using namespace std;
 int main ()
{
cout<<"Content-Type: text/html\n\n";    
cout<<"hi";
   return 0;
}
//编译
g++ a.cpp -o a.cgi

 

访问 http://192.168.43.8/cgi-bin/a.cgi

 

测试时出现500错误,查看错误日志显示:End of script output before headers

原因:我没加下面这句

cout<<"Content-Type: text/html\n\n";  

html 与 c 交互 (get传值给c)

在/var/www/html  下建一个cgi.html文件

内容如下

<body> 
<form name="form1" action="/cgi-bin/pass.cgi" method="get"> 
<table align="center"> 
        <tr><td align="center" colspan="2"></td></tr> 
        <tr> 
             <td align="right">用户名</td> 
             <td><input type="text" name="Username"></td> 
        </tr> 
        <tr> 
             <td align="right">密  码</td> 
             <td><input type="password" name="Password"></td> 
        </tr> 
        <tr> 
             <td><input type="submit" value="登    录"></td> 
             <td><input type="reset" value="取    消"></td> 
        </tr> 
</table> 
</form> 
</body>

在/var/www/cgi-bin  建一个pass.c , 然后编译为pass.cgi    gcc pass.c -o pass.cgi

 #include <stdio.h> 
 #include <stdlib.h> 
 #include <string.h> 
 
 char* getcgidata(FILE* fp, char* requestmethod); 
int main() 
{ 
                char *input; 
                char *req_method; 
                char name[64]; 
                char pass[64]; 
                int i = 0; 
                int j = 0; 
                
 //         printf("Content-type: text/plain; charset=iso-8859-1\n\n"); 
                printf("Content-type: text/html\n\n"); 
                printf("The following is query reuslt:<br><br>"); 
 
                req_method = getenv("REQUEST_METHOD"); 
                input = getcgidata(stdin, req_method); 
 
                // 我们获取的input字符串可能像如下的形式 
                // Username="admin"&Password="aaaaa" 
                // 其中"Username="和"&Password="都是固定的 
                // 而"admin"和"aaaaa"都是变化的,也是我们要获取的 
                
                // 前面9个字符是UserName= 
                // 在"UserName="和"&"之间的是我们要取出来的用户名 
                for ( i = 9; i < (int)strlen(input); i++ ) 
                { 
                             if ( input[i] == '&' ) 
                             { 
                                            name[j] = '\0'; 
                                            break; 
                             }                                     
                             name[j++] = input[i]; 
                } 
 
                // 前面9个字符 + "&Password="10个字符 + Username的字符数 
                // 是我们不要的,故省略掉,不拷贝 
                for ( i = 19 + strlen(name), j = 0; i < (int)strlen(input); i++ ) 
                { 
                             pass[j++] = input[i]; 
                } 
                pass[j] = '\0'; 
 
                printf("Your Username is %s<br>Your Password is %s<br> \n", name, pass); 
                
                return 0; 
} 
 
 char* getcgidata(FILE* fp, char* requestmethod) 
{ 
                char* input; 
                int len; 
                int size = 1024; 
                int i = 0; 
                
                if (!strcmp(requestmethod, "GET")) 
                { 
                             input = getenv("QUERY_STRING"); 
                             return input; 
                } 
                else if (!strcmp(requestmethod, "POST")) 
                { 
                             len = atoi(getenv("CONTENT_LENGTH")); 
                             input = (char*)malloc(sizeof(char)*(size + 1)); 
                             
                             if (len == 0) 
                             { 
                                            input[0] = '\0'; 
                                            return input; 
                             } 
                             
                             while(1) 
                             { 
                                            input[i] = (char)fgetc(fp); 
                                            if (i == size) 
                                            { 
                                                         input[i+1] = '\0'; 
                                                         return input; 
                                            } 
                                            
                                            --len; 
                                            if (feof(fp) || (!(len))) 
                                            { 
                                                         i++; 
                                                         input[i] = '\0'; 
                                                         return input; 
                                            } 
                                            i++; 
                                            
                             } 
                } 
                return NULL; 
}

记得给cgi.html  赋予777或755权限,我修改了半天文件,文件就是没更新,原因就是没给权限

访问http://192.168.43.8/cgi.html

 

 输入 admin  123456

 

 完成了c的动态web实现

c++  调用cgicc库 来更加简便的实现web

cgicc 稳定版本3.2.16: http://ftp.gnu.org/gnu/cgicc/cgicc-3.2.16.tar.gz

$ tar xzf cgicc-X.X.X.tar.gz 
$ cd cgicc-X.X.X/ 
$ ./configure --prefix=/usr 
$ make
$ make install

注意:libcgicc.so 和 libcgicc.a 库会被安装到/usr/lib目录下,需执行拷贝命令:

$ sudo cp /usr/lib/libcgicc.* /usr/lib64/

才能使 CGI 程序自动找到 libcgicc.so 动态链接库。

先熟悉cgicc库的各种类,方法和属性

https://www.cnblogs.com/aquester/archive/2018/12/04/10064335.html

cgicc官方开发文档:http://www.gnu.org/software/cgicc/doc/lib_overview.html

https://www.cnblogs.com/skawu/p/11069290.html

CGICC由两大部分组成:

  1) CGI输入处理子模块(CGI classes):

    cgicc::Cgicc 是cgicc库的主类。它用于检索特定HTML表单元素(如复选框、单选按钮和文本字段),上载文件的信息,以及保存,还原和检索CGI环境的信息和组:

    cgicc::CgiEnvironment 封装从HTTP服务器传递到CGI应用程序的数据。这包括由CGI标准中指定的HTTP服务器设置的所有环境变量。

    cgicc::FormEntry是一个不可变的类,表示HTML表单元素(如文本字段、单选按钮或复选框)中的单个用户条目。FormEntry本质上是一个名称/值对,其中名称是HTML表单本身中指定的表单元素的名称,值是用户输入的或用户选择的值。FormEntry提供了允许以字符串、整数或双精度形式访问值的方法。

    cgicc::FormFile是一个不可变的类,表示通过HTTP文件上载机制上载的文件。FormFile与FormEntry非常相似,但没有提供以不同类型访问值的众多方法。

  2) 响应生成子模块(Response generation classes):

    1) http header classes

cgicc::HTTPCookie是一个名称/值对,用于使用调用方自己的计算机存储调用方的一段信息。cookie通常被用作识别用户的一种手段。cgicc的任何头类都可能包含任意数量的cookie

cgicc::HTTPHeader 是所有简单HTTP头的基类。很少直接使用;相反,使用提供的子类之一。

cgicc::HTTPContentHeader 继承 cgicc::HTTPHeader 用于指示CGI应用程序返回给客户端的数据类型。

cgicc::HTTPRedirectHeader 继承cgicc::HTTPHeader 用于将客户端重定向到其他URL。

cgicc::HTTPStatusHeader 继承 cgicc::HTTPHeader 用于返回3位HTTP状态码和相关消息。

cgicc::HTTPHTMLHeader 继承 cgicc::HTTPContentHeader 用于MIME类型text/html的数据。

cgicc::HTTPResponseHeader 是一个更强大的通用HTTP头类,用于构造完整的HTTP响应。

 

 

 

    1) HTML generation classes

    HTML生成类用于在任何HTTP头之后生成HTML响应,由三部分组成。

 

      包含HTML版本信息的行

 

      声明性头段(由head元素分隔)

 

      包含文档实际内容的正文

 

cgicc定义了两个特殊的HTML类:
cgicc::HTMLDoctype用于指定HTML4.0标准所要求的HTML版本信息
cgicc::comment用于表示HTML注释

下面是输入处理子模块

 简单实现一个通过post登录

/var/www/html  下放置login.html 文件

<body> 
<form name="form1" action="/cgi-bin/text.cgi" method="post"> 
<table align="center"> 
        <tr><td align="center" colspan="2"></td></tr> 
        <tr> 
             <td align="right">用户名</td> 
             <td><input type="text" name="Username"></td> 
        </tr> 
        <tr> 
             <td align="right">密  码</td> 
             <td><input type="password" name="Password"></td> 
        </tr> 
        <tr> 
             <td><input type="submit" value="登    录"></td> 
             <td><input type="reset" value="取    消"></td> 
        </tr> 
</table> 
</form> 
</body>

/var/www/cgi-bin  下放置 text.cpp文件

#include <iostream>
#include <string>
#include <cgicc/CgiDefs.h> 
#include <cgicc/Cgicc.h> 
#include <cgicc/HTTPHTMLHeader.h> 
#include <cgicc/HTMLClasses.h>  
using namespace std;
using namespace cgicc;
int main ()
{
cout<<"Content-Type: text/html\n\n";    
Cgicc cgi;
form_iterator username = cgi.getElement("Username");
form_iterator password = cgi.getElement("Password");
if(username->getValue()=="admin"){
    if(password->getValue()=="123456")
        cout<< "login success!!";
  else
    cout<< "password error"; }
else cout<< "error login"; return 0; }
g++ -o text.cgi text.cpp -l cgicc

这里介绍cgi类下的 getElement()函数,传入html的name

 getElement() 实现了方法重载,上面我们使用的是第一个,返回form_iterator

form_iterator 和const_form_iterator 都是FormEntry的别名

通过调用FormEntry 类中的getValue()函数即可得到输入框的值,然后再做比较

下面通过连接mysql,来对用户密码确认

c++连接mysql 需要 mysql-server , mysql-client, mysql-devel

然后创建用户,数据库等

下面为简单的检测是否连接数据库

#include<iostream>
#include<mysql/mysql.h>
using namespace std;
int main(){
    MYSQL conn;
    mysql_init(&conn);
 if(mysql_real_connect(&conn,"localhost","bin","bin","bin",0,NULL,CLIENT_FOUND_ROWS)) //"root":数据库管理员 "":root密码 "test":数据库的名字
    {
        cout<<"Content-Type: text/html\n\n";  
        cout<<"connect success!";
    }
}
编译
g++ `mysql_config --cflags --libs` -o mysql.cgi mysql.cpp

更深一步,就是增删改查,更多mysql语句请参考:https://www.cnblogs.com/CloudComputing-binbin/p/14470008.html

 下面是结合c++ msyql

参考:

c++ 使用mysql查询结果为空的解决方法:https://blog.csdn.net/weixin_39616287/article/details/113686946

 mysql 查询语句测试

#include<iostream>
#include<mysql/mysql.h>
using namespace std;
int main(){
    MYSQL conn;
    MYSQL_RES *result;
    MYSQL_ROW row;    
    
    mysql_init(&conn);
    cout<<"Content-Type: text/html\n\n"; 
    if(mysql_real_connect(&conn,"localhost","bin","bin","bin",0,NULL,CLIENT_FOUND_ROWS)) //"root":数据库管理员 "":root密码 "test":数据库的名字
    {
        if(mysql_query(&conn,"select username from account where username='admin' and password='123456';"))  //执行sql
            cout<<"query fail...";
        else{
            cout<<"query success!!";
            result=mysql_store_result(&conn);
            int i=0;
            while((row=mysql_fetch_row(result))){      //获取每行
                i++;
                cout<<"<script>alert('welcome to "<<row[0]<<"')</script>";     
            }
            if(i==0)   //判断查询是否为空
                cout<<"<script>alert('"<<"username or password error..."<<"')</script>";
        }
    }
    else
        cout<<"connetc fail...";
}

mysql 与cgi 结合使用,html文件还是上面的文件

#include <iostream>
#include <string>
#include <mysql/mysql.h> 
#include <cgicc/CgiDefs.h> 
#include <cgicc/Cgicc.h> 
#include <cgicc/HTTPHTMLHeader.h> 
#include <cgicc/HTMLClasses.h>  
#include <cgicc/HTTPRedirectHeader.h>
using namespace std;
using namespace cgicc;
int main ()
{
    cout<<"Content-Type: text/html\n\n";   
    Cgicc cgi;
    form_iterator username = cgi.getElement("Username");
    form_iterator password = cgi.getElement("Password");
    string sql="select username from account where username='";
    sql+=username->getValue();        //getValue() 的返回值为string 
    sql+="' and password='";
    sql+=password->getValue();
    sql+="';";
    const char* sql_=sql.c_str();
    
    MYSQL conn;
    MYSQL_RES *result;
    MYSQL_ROW row;    
    
    mysql_init(&conn);
    if(mysql_real_connect(&conn,"localhost","bin","bin","bin",0,NULL,CLIENT_FOUND_ROWS)) //"root":数据库管理员 "":root密码 "test":数据库的名字
    {
        if(mysql_query(&conn,sql_))        //执行sql 
            cout<<"query fail...";
        else{
            cout<<"query success!!";
            result=mysql_store_result(&conn);
            int i=0;
            while((row=mysql_fetch_row(result))){
                i++;
                cout<<"<script>alert('welcome to "<<row[0]<<"')</script>"; 
            }
            if(i==0)             //通过查询出行的计数,来判断是否查询成功 
                cout<<"<script>alert('"<<"username or password error..."<<"')</script>";
                cout<<"<script>window.location.href='http://118.195.139.82/login.html'</script>"; //登录失败,跳转登录页面 
        }
    }
    else
        cout<<"connetc fail...";

}
g++ `mysql_config --cflags --libs` -l cgicc -o text.cgi text.cpp

 最后就是cgicc + mysql + cookie 实现登录

cookie的学习参考菜鸟教程:  https://www.runoob.com/cplusplus/cpp-web-programming.html

下面是实现的主类  header.h

#include <iostream>
#include <string>
#include <mysql/mysql.h> 
#include <cgicc/CgiDefs.h> 
#include <cgicc/Cgicc.h> 
#include <cgicc/HTTPHTMLHeader.h> 
#include <cgicc/HTMLClasses.h>  
#include <cgicc/HTTPRedirectHeader.h>
#include <stdio.h>    //sprintf()    需要用到该库 
#include <sstream>    //用于整型转字符串 
#include <cstdlib>    //用于随机数 
#include <openssl/sha.h>    //哈希加密 
using namespace std;
using namespace cgicc;

class main_{
    public:
        int login(){
            Cgicc cgi;
            form_iterator username = cgi.getElement("Username");
            form_iterator password = cgi.getElement("Password");
            string sql="select username from account where username='";
            sql+=username->getValue();        //getValue() 的返回值为string 
            sql+="' and password='";
            sql+=password->getValue();
            sql+="';";
            const char* sql_=sql.c_str();
    
            MYSQL conn;
            MYSQL_RES *result;
            MYSQL_ROW row;    
    
            mysql_init(&conn);
            if(mysql_real_connect(&conn,"localhost","bin","bin","bin",0,NULL,CLIENT_FOUND_ROWS)) //"root":数据库管理员 "":root密码 "test":数据库的名字
            {
                if(mysql_query(&conn,sql_)){}        //执行sql ,执行失败返回1 
                    //cout<<"query fail...";
                else{
                    //cout<<"query success!!";
                    result=mysql_store_result(&conn);
                    int i=0;
                    while((row=mysql_fetch_row(result))){
                        i++;
                        setcookie(username->getValue(),password->getValue());
                        cout<<"<script>alert('welcome to "<<row[0]<<"')</script>"; 
                        return 1;
                    }
                    if(i==0){             //通过查询出行的计数,来判断是否查询成功 
                        cout<<"Content-Type: text/html\n\n";        //这句要加上,不然就出现解析错误了,alert语句就显示不了 
                        cout<<"<script>alert('"<<"username or password error..."<<"')</script>";
                        cout<<"<script>window.location.href='http://118.195.139.82/login.html'</script>"; //登录失败,跳转登录页面 
                        return 0;
                    }
                }
            }
            else{
                cout<<"Content-Type: text/html\n\n";
                cout<<"connetc fail...";
            }

        }
        void setcookie(string username,string password){
            string sessid="Set-Cookie:Sessid=";
            sessid+=set_cookie_sha(username,password);
            sessid+=";";
            cout<<sessid;
            cout<<"Content-Type: text/html\n\n";         //cookie 要在c 
        }
        int check_cookie(){
            MYSQL conn;
            MYSQL_RES *result;
            MYSQL_ROW row;
            mysql_init(&conn);
            mysql_real_connect(&conn,"localhost","bin","bin","bin",0,NULL,CLIENT_FOUND_ROWS);
            string sql="select * from cookie_time where cookie='";
            
            Cgicc cgi;
            const_cookie_iterator cci;
            const CgiEnvironment& env = cgi.getEnvironment(); 
            for( cci = env.getCookieList().begin();cci != env.getCookieList().end(); ++cci ){
                if(cci->getName()=="Sessid"){
                    sql+=cci->getValue();
                    sql+="' limit 1;";
                    mysql_query(&conn,sql.c_str());
                    result=mysql_store_result(&conn);
                    int i=0;
                    while((row=mysql_fetch_row(result))){
                        i++;
                    }    
                    if(i==1){
                        //cout<<"success! ";
                        return 1;
                    }
                    else
                        return 0;
                }
            }
        }
        string set_cookie_sha(string username,string password){
            stringstream str_t;
            stringstream str_r;
            int t=time(0);    
            srand(t);
            int rand=random();
            str_t << t;
            str_r << rand;
            string str_time=str_t.str();
            string str_rand=str_r.str();
            //cout<<str_time<<str_rand<<"admin123456"<<endl;
            string cookie=str_time+str_rand+username+password;    //时间戳+随机数+用户名密码 
            //cout << sha256("dfs") << endl;
            string sha_cookie=sha256(cookie);
            insert_cookie(sha_cookie,str_time);
            return sha_cookie;
        }
        void insert_cookie(string cookie,string str_time){
            string sql="insert into cookie_time(cookie,time)values('";
            sql+=cookie;
            sql+="',";
            sql+=str_time;
            sql+=");";
            const char* sql_=sql.c_str();
            
            MYSQL conn;
            MYSQL_RES *result;
            MYSQL_ROW row;    
    
            mysql_init(&conn);
            if(mysql_real_connect(&conn,"localhost","bin","bin","bin",0,NULL,CLIENT_FOUND_ROWS)){
                if(mysql_query(&conn,sql_)){}
            }
        }
        string sha256(const string str){
            char buf[2];
            unsigned char hash[SHA256_DIGEST_LENGTH];
            SHA256_CTX sha256;
            SHA256_Init(&sha256);
            SHA256_Update(&sha256, str.c_str(), str.size());
            SHA256_Final(hash, &sha256);
            string NewString = "";
            for(int i = 0; i < SHA256_DIGEST_LENGTH; i++){
                sprintf(buf,"%02x",hash[i]);
                NewString = NewString + buf;
            }
            return NewString;
        }
};

 

check.cpp  //调用类的login() 来检测登录密码, 直接访问会出错

#include <iostream>
#include "header.h"
int main ()
{
    main_ a;
    if(a.login()==1){
        cout<<"<script>window.location.href='http://118.195.139.82/cgi-bin/home.cgi'</script>";
    }
        

}

 

home.cpp  //这就是通过登录后的系统界面, 直接访问没有cookie或cookie错误的会页面跳转到登陆界面,

#include "header.h"
int main(){
    cout << "Content-type:text/html\n\n";
    main_ ma;
    if(ma.check_cookie()==0){
        cout<<"<script>alert('"<<"please login..."<<"')</script>";
        cout<<"<script>window.location.href='http://118.195.139.82/login.html'</script>";
        return 0;
    }
    cout<<"good night!";
}

 

 login.html

<body> 
<form name="form1" action="/cgi-bin/check.cgi" method="post"> 
<table align="center"> 
        <tr><td align="center" colspan="2"></td></tr> 
        <tr> 
             <td align="right">用户名</td> 
             <td><input type="text" name="Username"></td> 
        </tr> 
        <tr> 
             <td align="right">密  码</td> 
             <td><input type="password" name="Password"></td> 
        </tr> 
        <tr> 
             <td><input type="submit" value="登    录"></td> 
             <td><input type="reset" value="取    消"></td> 
        </tr> 
</table> 
</form> 
</body>

 

g++ `mysql_config --cflags --libs` -l cgicc -l ssl -l crypto -o check.cgi check.cpp
g++ `mysql_config --cflags --libs` -l cgicc -l ssl -l crypto -o home.cgi home.cpp

 

 实现:

cookie是用 时间戳+随机数+用户密码 的sha加密

cookie 以及cookie授予的时间存入数据库中,创建事件,过期的cookie将被删除

检查cookie是用查询的,没做任何过滤,存在太low的sql注入了

 cookie也是

 

 

想了想,盲注不照样爆库么,限定字符只有数字字母,这样应该就全防吧

 还有创建定时删除过期cookie事务

参考:https://www.cnblogs.com/wang-yaz/p/12424858.html

开启mysql事务功能,永久开启方法:/etc/my.cnf中[mysqld]添加event_scheduler=on #重启服务

create table cookie_time(cookie varchar(100),time int);
create event de_cookie on schedule every 5 second do delete from cookie where time<(unix_timestamp(now())-60);    //每五秒删除前一分钟的数据
create event de_cookie on schedule every 1 hour do delete from cookie where time<(unix_timestamp(now())-14400); //每一小时删除前4小时的数据

 c++ 哈希加密:https://www.cnblogs.com/CloudComputing-binbin/p/14754486.html

 

 注册

register.html

#include"header.h"
int main(){
    cout << "Content-type:text/html\n\n";
    
    Cgicc cgi;
    form_iterator password = cgi.getElement("Password");
    form_iterator password_check = cgi.getElement("Password_check");
    if(password->getValue()!=password_check->getValue()){    //两次密码是否正确 
        cout<<"<!DOCTYPE html><html><head><link rel='stylesheet' type='text/css' href='http://www.huangwx.cn/css/sweetalert.css'> <script type='text/javascript' src='http://www.huangwx.cn/js/sweetalert-dev.js'></script></head>";
        cout<<"<body><script> swal({ title: 'Password Not Match',text:'Check your password again',type: 'error',confirmButtonColor: '#DD6B55',confirmButtonText: 'Retry', closeOnConfirm: false, closeOnCancel: false},function(isConfirm){ if (isConfirm){window.location.href='http://118.195.139.82/register.html';}});</script></body></html>";
        return 0;
    }
    
    form_iterator username = cgi.getElement("Username");
    main_bypass bypass;
    if(bypass.bypass(username->getValue())==0||bypass.bypass(password->getValue())==0){    //过滤输入值,只允许字母和数字 
        cout<<"<!DOCTYPE html><html><head><link rel='stylesheet' type='text/css' href='http://www.huangwx.cn/css/sweetalert.css'> <script type='text/javascript' src='http://www.huangwx.cn/js/sweetalert-dev.js'></script></head>";
        cout<<"<body><script> swal({ title: 'Forbidden Character',text:'Letters and numbers only allowed',type: 'error',confirmButtonColor: '#DD6B55',confirmButtonText: 'Retry', closeOnConfirm: false, closeOnCancel: false},function(isConfirm){ if (isConfirm){window.location.href='http://118.195.139.82/login.html';}});</script></body></html>";
        return 0;
    }else if(bypass.bypass(username->getValue())==2||bypass.bypass(password->getValue())==2){
        cout<<"Content-Type: text/html\n\n";
        cout<<"<!DOCTYPE html><html><head><link rel='stylesheet' type='text/css' href='http://www.huangwx.cn/css/sweetalert.css'> <script type='text/javascript' src='http://www.huangwx.cn/js/sweetalert-dev.js'></script></head>";
        cout<<"<body><script> swal({ title: 'Character Too Short',text:'5~20 characters only',type: 'error',confirmButtonColor: '#DD6B55',confirmButtonText: 'Retry', closeOnConfirm: false, closeOnCancel: false},function(isConfirm){ if (isConfirm){window.location.href='http://118.195.139.82/register.html';}});</script></body></html>";
        return 0;
    }
    
    MYSQL conn;        //连个数据库 
    mysql_init(&conn);
    mysql_real_connect(&conn,"localhost","bin","bin","bin",0,NULL,CLIENT_FOUND_ROWS);
    
    //查询是否注册过 
    MYSQL_RES *result;    
    MYSQL_ROW row;
    string sql_one="select username from account where username='"+username->getValue()+"';";
    mysql_query(&conn,sql_one.c_str());
    result=mysql_store_result(&conn);
    int i=0;
    while((row=mysql_fetch_row(result))){
        i++;
    }
    if(i==1){        
        cout<<"<!DOCTYPE html><html><head><link rel='stylesheet' type='text/css' href='http://www.huangwx.cn/css/sweetalert.css'> <script type='text/javascript' src='http://www.huangwx.cn/js/sweetalert-dev.js'></script></head>";
        cout<<"<body><script> swal({ title: 'Already Register',text:'Please login',type: 'info',confirmButtonColor: '#DD6B55',confirmButtonText: 'login', closeOnConfirm: false, closeOnCancel: false},function(isConfirm){ if (isConfirm){window.location.href='http://118.195.139.82/login.html';}});</script></body></html>";
        return 0;
    }
    
    //检验验证码 
    form_iterator qqmail = cgi.getElement("qqmail");
    form_iterator code = cgi.getElement("code");
    if(bypass.bypass_code(code->getValue())==0||bypass.bypass_qqmail(qqmail->getValue())==0){
        cout<<"<!DOCTYPE html><html><head><link rel='stylesheet' type='text/css' href='http://www.huangwx.cn/css/sweetalert.css'> <script type='text/javascript' src='http://www.huangwx.cn/js/sweetalert-dev.js'></script></head>";
        cout<<"<body><script> swal({ title: 'Identifying Code Error',type: 'error',confirmButtonColor: '#DD6B55',confirmButtonText: 'Retry', closeOnConfirm: false, closeOnCancel: false},function(isConfirm){ if (isConfirm){window.location.href='http://118.195.139.82/register.html';}});</script></body></html>";
        return 0;
    }
    
    string sql_two="select code from qqmail_code where qqmail='"+qqmail->getValue()+"';"; 
    mysql_query(&conn,sql_two.c_str());
    result=mysql_store_result(&conn);
    i=0;
    while((row=mysql_fetch_row(result))){
        if(row[0]==code->getValue()){
            i++;
            break;
        }
    }
    if(i==0){
        cout<<"<!DOCTYPE html><html><head><link rel='stylesheet' type='text/css' href='http://www.huangwx.cn/css/sweetalert.css'> <script type='text/javascript' src='http://www.huangwx.cn/js/sweetalert-dev.js'></script></head>";
        cout<<"<body><script> swal({ title: 'Identifying Code Error',type: 'error',confirmButtonColor: '#DD6B55',confirmButtonText: 'Retry', closeOnConfirm: false, closeOnCancel: false},function(isConfirm){ if (isConfirm){window.location.href='http://118.195.139.82/register.html';}});</script></body></html>";
        return 0;
    }
    
    //插入用户密码 
    string sql_three="insert into account(username,password,qqmail)values('"+username->getValue()+"','"+password->getValue()+"','"+qqmail->getValue()+"');"; 
    mysql_query(&conn,sql_three.c_str());
    
    cout<<"<!DOCTYPE html><html><head><link rel='stylesheet' type='text/css' href='http://www.huangwx.cn/css/sweetalert.css'> <script type='text/javascript' src='http://www.huangwx.cn/js/sweetalert-dev.js'></script></head>";
    cout<<"<body><script> swal({ title: 'Register Successful',type: 'success',confirmButtonColor: '#DD6B55',confirmButtonText: 'login', closeOnConfirm: false, closeOnCancel: false},function(isConfirm){ if (isConfirm){window.location.href='http://118.195.139.82/login.html';}});</script></body></html>";
                        
    return 0;
    
}

 

 

 

参考:

linux下c++编程基础:https://blog.csdn.net/qq_33750826/article/details/83868555

让apache支持cgi:https://www.txisfine.cn/archives/37d1a4ec.html#%E9%85%8D%E7%BD%AE%E8%BF%87%E7%A8%8B

c++ cgi 菜鸟教程:https://www.runoob.com/cplusplus/cpp-web-programming.html

开源C加加版本CGI库CGICC入门.pdf:

https://www.cnblogs.com/aquester/archive/2018/12/04/10064335.html

https://files-cdn.cnblogs.com/files/aquester/%E5%BC%80%E6%BA%90C%E5%8A%A0%E5%8A%A0%E7%89%88%E6%9C%ACCGI%E5%BA%93CGICC%E5%85%A5%E9%97%A8.pdf

cgicc库官方开发文档:http://www.gnu.org/software/cgicc/doc/lib_overview.html

c++ char* , const char* , string 的相互转换:https://www.cnblogs.com/wuyepeng/p/9729943.html

 

posted @ 2021-05-08 20:24  binbin_cloud  阅读(684)  评论(0编辑  收藏  举报