2023-09-22 15:15:17 +08:00
|
|
|
/**
|
|
|
|
* 该文件用于匹配login.html
|
|
|
|
* 使用该.c文件可实现从名为router的数据库中
|
|
|
|
* 查询存有登录名及密码的users表
|
|
|
|
*/
|
|
|
|
|
2023-09-19 18:44:20 +08:00
|
|
|
#include <cjson/cJSON.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <mysql/mysql.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-09-20 16:10:47 +08:00
|
|
|
int main() {
|
2023-09-19 18:44:20 +08:00
|
|
|
// 连接到 MySQL 数据库
|
|
|
|
MYSQL *conn;
|
|
|
|
MYSQL_RES *res;
|
|
|
|
MYSQL_ROW row;
|
|
|
|
|
|
|
|
conn = mysql_init(NULL);
|
2023-09-20 16:10:47 +08:00
|
|
|
if (conn == NULL) {
|
2023-09-19 18:44:20 +08:00
|
|
|
fprintf(stderr, "mysql_init() failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-09-23 16:27:09 +08:00
|
|
|
if (mysql_real_connect(conn, "localhost", "flykhan", "1202", "router", 3306,
|
2023-09-20 16:10:47 +08:00
|
|
|
NULL, 0) == NULL) {
|
2023-09-19 18:44:20 +08:00
|
|
|
fprintf(stderr, "mysql_real_connect() failed\n");
|
|
|
|
mysql_close(conn);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取前端传递的 JSON 数据
|
|
|
|
char request_data[128] = "";
|
|
|
|
fgets(request_data, 128, stdin);
|
|
|
|
|
|
|
|
// 解析 JSON 数据
|
|
|
|
cJSON *p = cJSON_Parse(request_data);
|
2023-09-20 16:10:47 +08:00
|
|
|
if (p == NULL) {
|
2023-09-19 18:44:20 +08:00
|
|
|
fprintf(stderr, "Error parsing JSON data\n");
|
|
|
|
mysql_close(conn);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取用户名和密码
|
|
|
|
cJSON *user = cJSON_GetObjectItemCaseSensitive(p, "name");
|
|
|
|
cJSON *pwd = cJSON_GetObjectItemCaseSensitive(p, "pwd");
|
|
|
|
|
|
|
|
if (user == NULL || pwd == NULL || !cJSON_IsString(user) ||
|
2023-09-20 16:10:47 +08:00
|
|
|
!cJSON_IsString(pwd)) {
|
2023-09-19 18:44:20 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Error getting username and/or password from JSON data\n");
|
|
|
|
cJSON_Delete(p);
|
|
|
|
mysql_close(conn);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *username_from_frontend = user->valuestring;
|
|
|
|
char *password_from_frontend = pwd->valuestring;
|
|
|
|
|
|
|
|
// 构建查询语句
|
|
|
|
char query[1000];
|
|
|
|
sprintf(query, "SELECT * FROM users WHERE username='%s' AND password='%s'",
|
|
|
|
username_from_frontend, password_from_frontend);
|
|
|
|
|
2023-09-20 16:10:47 +08:00
|
|
|
if (mysql_query(conn, query)) {
|
2023-09-19 18:44:20 +08:00
|
|
|
fprintf(stderr, "SELECT query failed. Error: %s\n", mysql_error(conn));
|
|
|
|
cJSON_Delete(p);
|
|
|
|
mysql_close(conn);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = mysql_store_result(conn);
|
2023-09-20 16:10:47 +08:00
|
|
|
if (res == NULL) {
|
2023-09-19 18:44:20 +08:00
|
|
|
fprintf(stderr, "mysql_store_result() failed\n");
|
|
|
|
cJSON_Delete(p);
|
|
|
|
mysql_close(conn);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON *result_json = cJSON_CreateObject();
|
|
|
|
|
2023-09-20 16:10:47 +08:00
|
|
|
if ((row = mysql_fetch_row(res))) {
|
2023-09-19 18:44:20 +08:00
|
|
|
cJSON_AddNumberToObject(result_json, "code", 0);
|
2023-09-20 15:36:23 +08:00
|
|
|
cJSON_AddStringToObject(result_json, "nickname", "admin");
|
2023-09-20 16:10:47 +08:00
|
|
|
} else {
|
2023-09-19 18:44:20 +08:00
|
|
|
cJSON_AddNumberToObject(result_json, "code", 1);
|
2023-09-21 12:01:05 +08:00
|
|
|
cJSON_AddStringToObject(result_json, "msg", "用户名或密码错误");
|
2023-09-19 18:44:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char *result_json_string = cJSON_Print(result_json);
|
|
|
|
|
|
|
|
// 输出 JSON 结果
|
|
|
|
printf("content-type: application/json;charset=utf-8\r\n");
|
|
|
|
printf("\r\n");
|
|
|
|
printf("%s\n", result_json_string);
|
|
|
|
|
|
|
|
free(result_json_string);
|
|
|
|
cJSON_Delete(result_json);
|
|
|
|
cJSON_Delete(p);
|
|
|
|
mysql_free_result(res);
|
|
|
|
mysql_close(conn);
|
|
|
|
|
|
|
|
return 0;
|
2023-09-21 12:01:05 +08:00
|
|
|
}
|