繁体   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