【问题标题】:C/HTML: Correctly printing the username and password input of the userC/HTML:正确打印用户输入的用户名和密码
【发布时间】:2016-10-27 14:48:11
【问题描述】:

几天前我开始尝试使用 C/CGI/HTML。我被看到的一些登录页面程序激怒了,所以我尝试自己做:

这是我的程序:

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

int main(void)
{
  char *data;
  char *user;
  char *password;
  printf("Content-type:text/html\r\n\r\n");
  printf("<!DOCTYPE html><html><head><title>Hello</title></head><body>");
  data = getenv("QUERY_STRING");
  if (data) {
    sscanf(data,"user=%s&password=%s", user, password);
    printf("Hello Mr./Ms. %s\n",user);
    printf("You entered your password %s\n",password);
  }
  printf("<form action='http://localhost/10.html'>");
  printf("<input type=text name=user>");
  printf("<input type=password name =password>");
  printf("</body></html>");
  exit(EXIT_SUCCESS);
 }

每当我执行我的 HTML 文件时:

输入:

用户名 = 123 密码 = 123

这是这个程序给我的输出:

 Hello Mr./Ms. 123&password=123 You entered your password (null) 

知道如何解决这个问题吗?谢谢!

【问题讨论】:

  • 哪个输入?这个程序的唯一输入来自环境变量QUERY_STRING,你没有提到这个环境变量包含什么。

标签: html c forms cgi


【解决方案1】:

在你的代码中

char *user;
char *password;

user 和 password 只是未初始化的指针。

你需要这个:

char user[100];
char password[100];

但很可能还有其他 CGI 和 sscanf 相关问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多