Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions CGImysql/id_passwd.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
qgy qgy
123 123
wts 123
wrx wrx
xxx xxx
yyy yyy
456 567
134 134
xq xq
xxxx xxxx
xqq xqq
%E5%95%8A a
gsa gsa
bhu bhu
596 596
256 256
098 098
qgy1 qgy1
qgy0 qgy0
qgy99 qgy99
qgy9 qgy9
ly9 ly9
qgy qgy
qgy qgy
qgy78 qgy78
qgy76 qgy76
qgy789 qgy789
yx0 yx1
qgyqgyqgy qgyqgyqgy
xme xme
3698 3695
0693 0693
3698 3698
3697 3697
twomonkeysclub 123
0328 0328
6129 6129
1996 1996
abc 123
0328 0328
0328 0328
0911 0911
0912 0912
123456 123456
jsdd 123456
1234 123
wxg wxg
rrr rrr
234 234
199 changes: 123 additions & 76 deletions CGImysql/sign.cpp
Original file line number Diff line number Diff line change
@@ -1,90 +1,137 @@
#include<mysql/mysql.h>
#include<iostream>
#include<string>
#include<string.h>
#include<cstdio>
#include"sql_connection_pool.h"
#include<map>
#include <mysql/mysql.h>
#include <iostream>
#include <string>
#include <string.h>
#include <cstdio>
#include "sql_connection_pool.h"
#include <map>
#include <fstream>
#include <sstream>
using namespace std;

int main(int argc,char *argv[])
int main(int argc, char *argv[])
{
map<string,string> users;
map<string, string> users;

pthread_mutex_t lock;
pthread_mutex_init(&lock, NULL);

//初始化数据库连接池,连接池为静态大小
//通过主机地址和登录账号,密码进入服务器数据库,选择qgydb库
connection_pool *connPool=connection_pool::GetInstance("localhost","root","root","qgydb",3306,5);
locker lock;
string SQLVerify(argv[3]);

//在连接池中取一个连接
MYSQL *mysql=connPool->GetConnection();
//在user表中检索username,passwd数据,浏览器端输入
if(mysql_query(mysql,"SELECT username,passwd FROM user"))
if ("2" == SQLVerify)
{
printf("INSERT error:%s\n",mysql_error(mysql));
return -1;
}
//从表中检索完整的结果集
MYSQL_RES *result=mysql_store_result(mysql);
//返回结果集中的列数
int num_fields=mysql_num_fields(result);
//返回所有字段结构的数组
MYSQL_FIELD *fields=mysql_fetch_fields(result);
string sql_user(argv[4]);
string sql_passwd(argv[5]);
string sql_name(argv[6]);

//从结果集中获取下一行,将对应的用户名和密码,存入map中
while(MYSQL_ROW row=mysql_fetch_row(result))
{
string temp1(row[0]);
string temp2(row[1]);
users[temp1]=temp2;
}
MYSQL *con = NULL;
con = mysql_init(con);

string name(argv[1]);
const char *namep = name.c_str();
string passwd(argv[2]);
const char *passwdp = passwd.c_str();
char flag = *argv[0];

//如果是注册,先检测数据库中是否有重名的
//没有重名的,进行增加数据
char *sql_insert = (char*)malloc(sizeof(char)*200);
strcpy(sql_insert, "INSERT INTO user(username, passwd) VALUES(");
strcat(sql_insert, "'");
strcat(sql_insert, namep);
strcat(sql_insert, "', '");
strcat(sql_insert, passwdp);
strcat(sql_insert, "')");

if(flag == '3'){
if(users.find(name)==users.end()){
if (con == NULL)
{
cout << "Error:" << mysql_error(con);
exit(1);
}
con = mysql_real_connect(con, "localhost", sql_user.c_str(), sql_passwd.c_str(), sql_name.c_str(), 3306, NULL, 0);

pthread_mutex_lock(&lock);
int res = mysql_query(mysql,sql_insert);
pthread_mutex_unlock(&lock);
if (con == NULL)
{
cout << "Error: " << mysql_error(con);
exit(1);
}

if(!res)
printf("1\n");
else
printf("0\n");
}
else
printf("0\n");
}
//如果是登录,直接判断
//若浏览器端输入的用户名和密码在表中可以查找到,返回1,否则返回0
else if(flag == '2'){
if(users.find(name)!=users.end()&&users[name]==passwd)
printf("1\n");
else
printf("0\n");
//在user表中检索username,passwd数据,浏览器端输入
if (mysql_query(con, "SELECT username,passwd FROM user"))
{
printf("INSERT error:%s\n", mysql_error(con));
return -1;
}
//从表中检索完整的结果集
MYSQL_RES *result = mysql_store_result(con);
//返回结果集中的列数
int num_fields = mysql_num_fields(result);
//返回所有字段结构的数组
MYSQL_FIELD *fields = mysql_fetch_fields(result);

//从结果集中获取下一行,将对应的用户名和密码,存入map中
while (MYSQL_ROW row = mysql_fetch_row(result))
{
string temp1(row[0]);
string temp2(row[1]);
users[temp1] = temp2;
}

string name(argv[1]);
const char *namep = name.c_str();
string passwd(argv[2]);
const char *passwdp = passwd.c_str();
char flag = *argv[0];

//如果是注册,先检测数据库中是否有重名的
//没有重名的,进行增加数据
char *sql_insert = (char *)malloc(sizeof(char) * 200);
strcpy(sql_insert, "INSERT INTO user(username, passwd) VALUES(");
strcat(sql_insert, "'");
strcat(sql_insert, namep);
strcat(sql_insert, "', '");
strcat(sql_insert, passwdp);
strcat(sql_insert, "')");

if (flag == '3')
{
if (users.find(name) == users.end())
{
lock.lock();
int res = mysql_query(con, sql_insert);
lock.unlock();

if (!res)
printf("1\n");
else
printf("0\n");
}
else
printf("0\n");
}
//如果是登录,直接判断
//若浏览器端输入的用户名和密码在表中可以查找到,返回1,否则返回0
else if (flag == '2')
{
if (users.find(name) != users.end() && users[name] == passwd)
printf("1\n");
else
printf("0\n");
}
else
printf("0\n");
//释放结果集使用的内存
mysql_free_result(result);
}
else
printf("0\n");
//释放结果集使用的内存
mysql_free_result(result);
{
ifstream out(argv[2]);
string linestr;
while (getline(out, linestr))
{
string str;
stringstream id_passwd(linestr);

connPool->DestroyPool();
}
getline(id_passwd, str, ' ');
string temp1(str);

getline(id_passwd, str, ' ');
string temp2(str);
users[temp1] = temp2;
}
out.close();

//只完成登录
string name(argv[0]);
const char *namep = name.c_str();
string passwd(argv[1]);
const char *passwdp = passwd.c_str();

if (users.find(name) != users.end() && users[name] == passwd)
printf("1\n");
else
printf("0\n");
}
}
Loading