HTML表单和C-检查唯一用户名
我目前正在写我自己的网站,我试图确保当有人创建帐户时,用户名是唯一的。我正在用C做后端(因为我不知道php/js),我一直在运行中遇到一些问题。现在,我在一个文件newuser.txt(该文件只有唯一的用户名)中获取环境变量,如下所示: 全名=测试 描述=测试 用户名=测试 密码=测试 我知道在newusers.txt文件的第3、7、11行等处,我将获得我的用户名,因此我考虑将所有用户名添加到另一个文件(该文件也承载传入的数据),然后检查传入的用户名是否唯一,如果唯一,然后我想将所有数据(例如全名、用户名等)添加到newusers.txt。这是我的密码:HTML表单和C-检查唯一用户名,html,c,forms,Html,C,Forms,我目前正在写我自己的网站,我试图确保当有人创建帐户时,用户名是唯一的。我正在用C做后端(因为我不知道php/js),我一直在运行中遇到一些问题。现在,我在一个文件newuser.txt(该文件只有唯一的用户名)中获取环境变量,如下所示: 全名=测试 描述=测试 用户名=测试 密码=测试 我知道在newusers.txt文件的第3、7、11行等处,我将获得我的用户名,因此我考虑将所有用户名添加到另一个文件(该文件也承载传入的数据),然后检查传入的用户名是否唯一,如果唯一,然后我想将所有数据(例如全
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
int currentLine = 1;
char fileLine[100];
int searchLine= 3;
char input[200];
int i=0;
int n = atoi(getenv("CONTENT_LENGTH"));
fgets(input,n+1,stdin); //get the input from the form
printf("Content-Type:text/html\n\n");
printf("<TITLE>Account Creation Query</TITLE>\n");
if (n == 0)
{
printf("<p> Error. Please try again.</p>");
}
FILE *f = fopen("newusers.txt", "ab");
FILE *g = fopen("incoming.txt", "ab");
if (f == NULL)
{
printf("<p> Error in opening the file. Check if the file exists</p>");
printf("<p><a href=\"../login.html\">Login Page</a></p>");
printf("<p><a href=\"../home.html\">Homepage</a></p>");
}
else
{
while(fgets(fileLine, 100, f)) /*searching for the usernames and adding them to the incoming.txt file */
{
if(searchLine == currentLine)
{
fputs(fileLine, g);
searchLine = searchLine + 4;
}
currentLine++;
}
char *token = strtok(input, "&"); /*tokenizing the incoming data and adding it to the incoming.txt file */
while(token!=NULL)
{
fputs(token, g);
fputs("\n", g);
token = strtok(NULL, "&");
}
}
printf("<p> Account created successfully. You can now login!</p>");
printf("<p><a href=\"../login.html\">Login Page</a></p>");
fclose(f);
fclose(g);
return 0;
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
int currentLine=1;
char文件行[100];
int searchLine=3;
字符输入[200];
int i=0;
int n=atoi(getenv(“内容长度”);
fgets(input,n+1,stdin);//从表单获取输入
printf(“内容类型:text/html\n\n”);
printf(“帐户创建查询”);
如果(n==0)
{
printf(“错误。请重试。”;
}
文件*f=fopen(“newusers.txt”、“ab”);
FILE*g=fopen(“incoming.txt”、“ab”);
如果(f==NULL)
{
printf(“打开文件时出错。请检查文件是否存在””;
printf(“”);
printf(“”);
}
其他的
{
同时(fgets(fileLine,100,f))/*搜索用户名并将其添加到传入的.txt文件中*/
{
if(searchLine==currentLine)
{
FPUT(文件行,g);
搜索线=搜索线+4;
}
currentLine++;
}
char*token=strtok(输入“&”);/*标记传入数据并将其添加到incoming.txt文件*/
while(令牌!=NULL)
{
fputs(令牌,g);
fputs(“\n”,g);
令牌=strtok(空,&);
}
}
printf(“帐户创建成功。您现在可以登录!”;
printf(“”);
fclose(f);
fclose(g);
返回0;
}
理想情况下,此时传入的.txt文件如下所示:
firstname=bla
description=bla
用户名=bla
密码=bla
用户名=u1
用户名=u2
用户名=u3
现在我一直在比较传入用户名和其他用户名,然后将数据复制回newusers.txt。任何帮助都将不胜感激 使用system()调用调用grep binary进行搜索,而不是用C编写搜索代码
如下
//concat字符串将“grep filename username | cut-d'='-f2”
转换为cmdstring,然后
//阅读内容
in=popen(cmdstring, "r");
if(in==NULL)
{
perror("Shell execution error");
exit(EXIT_FAILURE);
}
fgets(buf,3000,in)!=NULL)
这里buf包含新用户的名称,然后它已经存在
否则,他选择了唯一的名称。我强烈建议您为这个项目学习一种脚本语言。PHP、Perl、Python、Ruby、Javascript……有很多选择,其中任何一种都比C更适合于web编程
也就是说,这里需要的是一个数据库。考虑使用或;两者都很容易从C进行交互,并且允许您执行查找和插入,这比使用平面文件(正如您在这里尝试的那样)要容易得多。对于搜索之类的任务,使用外部可执行文件是一个非常糟糕的主意。它不仅性能很差,而且很容易在无意中引入远程代码执行漏洞,例如使用包含shell元字符的用户名,如
或$(…)
。如果你擅长JavaScript,那么就选择nodejs和mangoDB而不是C。特别是对于web开发,非常感谢你的建议!)我一定要试试。