2016-10-27 131 views
0

我几天前开始尝试使用C/CGI/HTML。我被我看到了,所以我尝试做一些登录页面节目激起了我自己:C/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) 

任何想法如何解决这个问题?谢谢!

+0

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

回答

0

在你的代码

char *user; 
char *password; 

userpassword只是未初始化的指针。

你需要这样的:

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

但也有极有可能其他CGI和sscanf相关的问题。

相关问题