【发布时间】:2016-10-21 12:28:30
【问题描述】:
我们被要求使用 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 没有打印任何内容。这只是空白区域。如何解决此问题并能够打印用户在用户名和密码框中输入的内容?谢谢!
【问题讨论】:
-
您不会在解析查询字符串的循环内打印任何内容。在循环之后
token将是NULL(这是循环条件的一部分),您尝试打印该NULL指针。 -
@Joachim Pileborg 关于如何打印用户名和密码有什么建议吗?那我应该打印 strtok(data) 吗?我对此有点陌生,对不起:((