簡體   English   中英

CGI- HTML密碼無法打印

[英]CGI- Password from HTML won't print

我們被要求使用C,HTML,MySQL和CGI創建類似Twitter的程序。 第一步是創建登錄頁面,我們將要求用戶輸入用戶名和密碼。 為此,我使用了CGI x HTML,這是我的程序:

HTML:

<html>
  <body>
    <form action='/cgi-bin/password .cgi'>
    Username: <input type="text" name="user" ><br>
    Password: <input type="password" name ="password" id="password"  maxlength="10">
    <input type ="submit" value='Submit'>
    </form>
  </body>
</html>

CGI:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{

char *data;
char *token;
printf("Content-type:text/html\r\n\r\n");
printf("<!DOCTYPE html><html><head><title>Is Your Password and username this?<title></head><body>");
data = getenv("QUERY_STRING");
  if (data) {
        token = strtok(data, "&");
        while (token) {
              while (*token != '=') 
              {
              token++;
              }
          token++;
          token = strtok(NULL, "&");
          }
    printf("The average is %s\n", token);
  }
  printf("</body></html>");
  exit(EXIT_SUCCESS);
}

問題:輸入用戶名和密碼並按提交按鈕后,cgi沒有打印任何內容。 這只是空白。 如何解決此問題並能夠打印用戶名和密碼框中輸入的內容? 謝謝!

對於初學者,我建議您復制getenv獲得的字符串。 您永遠不要修改從getenv獲得的字符串,而strtok修改。

同樣,當您調用strtok ,您獲得的指針指向name=value對中名稱的開頭。 通過修改指針變量(使用token++ ),您將失去起點,並且將不再具有指向該名稱的指針。

然后,我建議您查看類似strchr的代碼,以簡化代碼,並且不要使用內部循環。

放在一起,如果可能的話,您可以做類似的事情

char *data_ptr = getenv("QUERY_STRING");
char data[strlen(data_ptr) + 1];  // +1 for the string terminator
strcpy(data, data_ptr);

char *name = strtok(data, "&");
while (name != NULL)
{
    char *value_sep = strchr(name, '=');
    if (value_sep != NULL)
    {
        *value_sep = '\0';
        char *value = ++value_sep;

        printf("Name = %s\r\n", name);
        printf("Value = %s\r\n", value);
    }
    else
    {
        printf("Malformed query string\r\n");
    }

    name = strtok(NULL, "&");
}

您可以在此處的“操作”中看到它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM