私は現在自分のウェブサイトを作成しており、誰かがアカウントを作っているときにユーザー名が一意であることを確認しようとしています。私はCでバックエンドをやっています(私はphp/jsを知らないので)、私はちょっとした問題を抱えています。今、私は、次のような(このファイルは唯一のユニークなユーザ名を持っている)ファイルnewuser.txtで環境変数を取得しています:HTMLフォームとC - 固有のユーザー名の確認
フルネーム=テスト
説明=テスト
ユーザ名=テスト
パスワード=テスト
newusers.txtファイルの3,7,11などの行に私のユーザー名が表示されるので、すべてのユーザー名を別のファイル(入力データもホストする)に追加することを考えました。入ってくるかどうかを確認してくださいusernameが一意である場合は、newusers.txtにすべてのデータ(完全な名前、ユーザー名など)を追加します。ここに私のコードは次のとおりです。
#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;
}
理想的には、この時点で私のincoming.txtファイルは次のようになります。
FIRSTNAME = BLA
説明= BLA
ユーザ名= BLA
パスワード= bla
ユーザー名= u1
ユーザ名= U2
ユーザ名= U3
...
今、私は、他のユーザ名に入ってくるユーザー名を比較して、バックnewusers.txtにデータをコピーするにこだわっています。どんな助けもありがとう!
JavaScriptでうまくいけば、Cの代わりにnodejsとmangoDBに行きます。特にWeb開発の場合 – kakurala