脚本输出结束 - CGI / HTML

时间:2016-10-29 12:20:49

标签: html c forms apache cgi

我们被要求在HTML / CGI中创建一个登录页面。我想出了这段代码:

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

char username[20];
char password[20];

char* extract_value(char* data,int data_len, char* name, char* dest);

int main()
{ 
    int size;
    char *buff;

    printf("Content-type:text/html\n\n");
    printf("<html><body>");
    buff = getenv("CONTENT_LENGTH");
    if (buff)
        size = atoi(buff);

    char data[size+1];
    int i;
    for(i=0;i<size;i++)
            data[i]=fgetc(stdin);
    data[size]='\0';

    extract_value(data,size+1,"username",username);
    extract_value(data,size+1,"password",password);

    printf("</body></html>");
}

char* extract_value(char* data,int data_len, char* name, char* dest)
{
    int i,j,k,flag,carry;
    for(i=0;i<data_len;i++){
        flag=1;
        for(j=0,k=0;name[j]!='\0';j++,k++)
        {
            if(j>=data_len)
            {
                i=data_len;
                break;
            }
            else if(name[j]!=data[i+k]){
                flag=0;
                break;
            }
           carry=i+k+2;
        }
        if(flag)
        {
        j=carry;
            for(k=0;data[j]!='&'&&data[j]!='\0';j++,k++)
            {
            dest[k]=data[j];
            }
        dest[k]='\0';
        break;
    }
}
return dest;
}

这是我的HTML程序:

<!DOCTYPE html>
<html>
<body>
<form action='/cgi-bin/11.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>

编译并通过Apache / XAMPP运行后,它出现了这个错误:

Server error!

The server encountered an internal error and was unable to complete your request.

Error message: 
End of script output before headers: 11.cgi

If you think this is a server error, please contact the webmaster.

知道如何解决这类问题吗?非常感谢!

1 个答案:

答案 0 :(得分:1)

尝试将printf("Content-type:text/html\n\n");更改为printf("Content-type:text/html\r\n\r\n");