简体   繁体   English

CGI- HTML密码无法打印

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

We were asked to create a twitter-like program using C, HTML, MySQL and CGI. 我们被要求使用C,HTML,MySQL和CGI创建类似Twitter的程序。 The first step is creating the login page wherein we would ask the user to enter their username and password. 第一步是创建登录页面,我们将要求用户输入用户名和密码。 I used CGI x HTML in doing that and here are my programs: 为此,我使用了CGI x HTML,这是我的程序:

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: 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);
}

PROBLEM: After entering the username and password and pressing the submit button, the cgi is not printing anything. 问题:输入用户名和密码并按提交按钮后,cgi没有打印任何内容。 It's just blank space. 这只是空白。 How do I fix this and be able to print what the user enetered in the username and password boxes? 如何解决此问题并能够打印用户名和密码框中输入的内容? Thanks! 谢谢!

For starters I suggest you copy the string you get from getenv . 对于初学者,我建议您复制getenv获得的字符串。 You should never modify the string you get from getenv , and strtok modifies it. 您永远不要修改从getenv获得的字符串,而strtok修改。

Also, when you call strtok the pointer you get is pointing to the beginning of the name in the name=value pair. 同样,当您调用strtok ,您获得的指针指向name=value对中名称的开头。 By modifying the pointer variable (which you do with token++ ) you lose the start and will not have a pointer to the name anymore. 通过修改指针变量(使用token++ ),您将失去起点,并且将不再具有指向该名称的指针。

Then I suggest you look at something like strchr to simplify the code and don't have the inner loop. 然后,我建议您查看类似strchr的代码,以简化代码,并且不要使用内部循环。

Putting it all together, if it is possible you could do something like 放在一起,如果可能的话,您可以做类似的事情

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, "&");
}

You can see it in "action" here . 您可以在此处的“操作”中看到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM