帮我写cgi登录和账号管理框架
时间: 2023-11-21 11:25:37 浏览: 51
登录并验证cgi 程序
4星 · 用户满意度95%
好的,下面是一个简单的C语言cgi登录和账号管理框架示例,仅供参考:
1. 登录页面(login.html)
```html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action="/cgi-bin/login.cgi" method="post">
<label>Username:</label>
<input type="text" name="username"><br>
<label>Password:</label>
<input type="password" name="password"><br>
<button type="submit">Login</button>
</form>
</body>
</html>
```
2. 登录CGI程序(login.cgi)
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR_LEN 256
// 用户结构体
typedef struct {
char username[MAX_STR_LEN];
char password[MAX_STR_LEN];
} User;
// 用户列表
User users[] = {
{"admin", "admin"},
{"user1", "password1"},
{"user2", "password2"},
{"user3", "password3"},
{"user4", "password4"},
};
// 用户数量
int n_users = sizeof(users) / sizeof(User);
// 检查用户名和密码是否匹配
int check_login(char *username, char *password) {
int i;
for (i = 0; i < n_users; i++) {
if (strcmp(username, users[i].username) == 0 &&
strcmp(password, users[i].password) == 0) {
return 1;
}
}
return 0;
}
int main() {
char *data;
char username[MAX_STR_LEN];
char password[MAX_STR_LEN];
// 读取数据
data = getenv("QUERY_STRING");
sscanf(data, "username=%[^&]&password=%s", username, password);
// 检查用户名和密码是否匹配
if (check_login(username, password)) {
// 登录成功,跳转到主页面
printf("Location: /cgi-bin/main.cgi\n\n");
} else {
// 登录失败,返回登录页面
printf("Content-type:text/html\n\n");
printf("<h2>Login Failed</h2>");
printf("<a href=\"/login.html\">Back to Login</a>");
}
return 0;
}
```
3. 账号管理页面(account.html)
```html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Account Management</title>
</head>
<body>
<h2>Account Management</h2>
<form action="/cgi-bin/account.cgi" method="post">
<label>Username:</label>
<input type="text" name="username"><br>
<label>Password:</label>
<input type="password" name="password"><br>
<label>Role:</label>
<select name="role">
<option value="admin">Admin</option>
<option value="user">User</option>
</select><br>
<button type="submit" name="action" value="add">Add</button>
<button type="submit" name="action" value="delete">Delete</button>
<button type="submit" name="action" value="modify">Modify</button>
</form>
</body>
</html>
```
4. 账号管理CGI程序(account.cgi)
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR_LEN 256
// 用户结构体
typedef struct {
char username[MAX_STR_LEN];
char password[MAX_STR_LEN];
char role[MAX_STR_LEN];
} User;
// 用户列表
User users[] = {
{"admin", "admin", "admin"},
{"user1", "password1", "user"},
{"user2", "password2", "user"},
{"user3", "password3", "user"},
{"user4", "password4", "user"},
};
// 用户数量
int n_users = sizeof(users) / sizeof(User);
// 添加用户
void add_user(char *username, char *password, char *role) {
if (n_users >= MAX_USERS) {
printf("Too many users\n");
exit(1);
}
strcpy(users[n_users].username, username);
strcpy(users[n_users].password, password);
strcpy(users[n_users].role, role);
n_users++;
}
// 删除用户
void delete_user(char *username) {
int i, j;
for (i = 0; i < n_users; i++) {
if (strcmp(username, users[i].username) == 0) {
for (j = i; j < n_users - 1; j++) {
users[j] = users[j + 1];
}
n_users--;
break;
}
}
}
// 修改用户密码
void modify_password(char *username, char *password) {
int i;
for (i = 0; i < n_users; i++) {
if (strcmp(username, users[i].username) == 0) {
strcpy(users[i].password, password);
break;
}
}
}
int main() {
char *data;
char username[MAX_STR_LEN];
char password[MAX_STR_LEN];
char role[MAX_STR_LEN];
char action[MAX_STR_LEN];
// 读取数据
data = getenv("QUERY_STRING");
sscanf(data, "username=%[^&]&password=%[^&]&role=%[^&]&action=%s",
username, password, role, action);
// 处理请求
if (strcmp(action, "add") == 0) {
add_user(username, password, role);
} else if (strcmp(action, "delete") == 0) {
delete_user(username);
} else if (strcmp(action, "modify") == 0) {
modify_password(username, password);
}
// 返回结果
printf("Content-type:text/html\n\n");
printf("<h2>Account Management</h2>");
printf("<table border=\"1\"><tr><th>Username</th><th>Password</th><th>Role</th></tr>");
int i;
for (i = 0; i < n_users; i++) {
printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>",
users[i].username, users[i].password, users[i].role);
}
printf("</table>");
printf("<a href=\"/account.html\">Back to Account Management</a>");
return 0;
}
```
以上是一个简单的C语言cgi登录和账号管理框架示例,仅供参考。实际应用中,需要根据具体需求进行修改和优化。同时,需要注意安全性和稳定性,避免数据泄露和程序崩溃等问题。
阅读全文