cgi_base login.html的修改

This commit is contained in:
chang 2023-10-25 14:04:49 +08:00
parent e0dc860f98
commit 9d07810e70
7 changed files with 82 additions and 13 deletions

View File

@ -1,7 +1,7 @@
all: all:
sqlite_cgi_base: sqlite_cgi_base:
gcc sqlite3.c sqlite_cgi_base.c -o sqlite_cgi_base -lpthread -ldl -lcjson gcc sqlite3.c sqlite_cgi_base.c -o sqlite_cgi_base.cgi -lpthread -ldl -lcjson
clean: clean:
rm -rf sqlite_cgi_base rm -rf *.cgi

Binary file not shown.

Binary file not shown.

View File

@ -3,6 +3,10 @@
// #include "cJSON.h" // #include "cJSON.h"
#include "sqlite3.h" #include "sqlite3.h"
// sqlite3的回调函数
// sqlite 每查到一条记录,就调用一次这个回调
int sql_search_callback(void* NotUsed, int argc, char** argv, char** azColName);
int main(int argc, char const* argv[]) { int main(int argc, char const* argv[]) {
// 连接到 SQLite 数据库 // 连接到 SQLite 数据库
sqlite3* db; // 声明 sqlite3 类型变量,即声明一个数据库对象 sqlite3* db; // 声明 sqlite3 类型变量,即声明一个数据库对象
@ -36,7 +40,7 @@ int main(int argc, char const* argv[]) {
cJSON* password = cJSON_GetObjectItemCaseSensitive(json_buf, "password"); cJSON* password = cJSON_GetObjectItemCaseSensitive(json_buf, "password");
if (user == NULL || password == NULL || !cJSON_IsString(user) || if (user == NULL || password == NULL || !cJSON_IsString(user) ||
!cJSON_IsString(password)) { !cJSON_IsString(password)) { // 是否是非法输入
fprintf(stderr, fprintf(stderr,
"Error getting username and/or password from JSON data\n"); "Error getting username and/or password from JSON data\n");
cJSON_Delete(json_buf); cJSON_Delete(json_buf);
@ -58,7 +62,9 @@ int main(int argc, char const* argv[]) {
username_from_frontend, password_from_frontend); // 构建查询语句 username_from_frontend, password_from_frontend); // 构建查询语句
// 执行查询语句 // 执行查询语句
rc = sqlite3_exec(db, query_sql, 0, 0, &zErrMsg); // 执行查询语句 // rc = sqlite3_exec(db, query_sql, 0, 0, &zErrMsg); // 执行查询语句
rc = sqlite3_exec(db, query_sql, sql_search_callback, 0,
&zErrMsg); // 执行查询语句
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg); fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg); sqlite3_free(zErrMsg);
@ -84,3 +90,8 @@ int main(int argc, char const* argv[]) {
return 0; return 0;
} }
int sql_search_callback(void* NotUsed,
int argc,
char** argv,
char** azColName) {}

View File

@ -129,7 +129,7 @@
<input name="name" placeholder="请输入账号"> <input name="name" placeholder="请输入账号">
</div> </div>
<div class="row"> <div class="row">
<input name="pwd" type="password" placeholder="请输入密码"> <input name="password" type="password" placeholder="请输入密码">
</div> </div>
<div class="row center"> <div class="row center">
<button>登录</button> <button>登录</button>
@ -145,9 +145,9 @@
function login(form) { function login(form) {
let login_data = { let login_data = {
name: form.name.value.trim(), name: form.name.value.trim(),
pwd: form.pwd.value.trim() password: form.password.value.trim()
}; };
fetch("../cgi-bin/login.cgi", { fetch("../cgi-bin/sqlite_cgi_base.cgi", {
method: "POST", method: "POST",
body: JSON.stringify(login_data), body: JSON.stringify(login_data),
headers: { headers: {
@ -159,12 +159,17 @@
window.localStorage.setItem("login_user", JSON.stringify(data)); window.localStorage.setItem("login_user", JSON.stringify(data));
open("/zhuti.html", "_self"); open("/zhuti.html", "_self");
} else { } else {
alert(data.msg); // alert(data.msg);
open("/zhuti.html", "_self");
} }
}) })
return false; return false;
} }
// 根据您提供的代码,这是一个用于登录验证的 JavaScript 脚本。它通过 fetch 函数向服务器发送一个 POST 请求,将登录表单中的用户名和密码作为 JSON 数据发送给服务器。
// 服务器应该有一个名为 login.cgi 的 CGI 脚本来处理登录请求。脚本会接收到 POST 请求,并验证用户名和密码是否正确。如果验证成功,服务器会返回一个 JSON 响应,其中
// code 字段为 0表示登录成功同时还会返回一些其他的用户信息。客户端会将这些信息存储在本地的 localStorage 中,并使用 open 函数打开 /zhuti.html 页面。
// 请确保服务器端的 login.cgi 脚本正确处理了登录请求,并返回正确的 JSON 响应。此外,还需要确保服务器端的路径和文件名与 JavaScript 代码中的路径和文件名一致。
</script> </script>
</body> </body>

View File

@ -1,14 +1,52 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html>
<head> <head>
<meta charset="UTF-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="keywords" content="">
<title>Document</title> <title>历史操作表</title>
</head> </head>
<body> <body>
operation_history <h1 align="center">历史操作表</h1>
<table id="arpTable" width="500" border="1" align="center">
<thead>
<th>用户名</th>
<th>操作指令</th>
<th>操作时间</th>
</thead>
<tbody id="arpBody" align="center">
</tbody>
</table>
<script>
window.onload = function () {
query("");
}
function query(val) {
fetch("/cgi-bin/router_showarp.cgi").then(resp =>
resp.json()).then(data => {
if (data.code == 0) {
arpBody.innerText = "";
data.data.forEach(rowData => {
let tr = document.createElement("tr");
let td1 = document.createElement("td");
let td2 = document.createElement("td");
td1.innerText = rowData.ip;
td2.innerText = rowData.mac;
tr.append(td1, td2);
arpBody.append(tr);
})
}
});
}
</script>
</body> </body>
</html> </html>

View File

@ -46,5 +46,20 @@
</div> </div>
</body> </body>
<script>
window.onload = function () {
if (localStorage.getItem("login_user") == null) {
open("/login.html", "_self");
} else {
let login_user_str = localStorage.getItem("login_user");
let login_user = JSON.parse(login_user_str);
userName.innerText = login_user.nickname;
}
}
function logout() {
localStorage.clear();
open("/login.html", "_self");
}
</script>
</html> </html>