Kpuonyer
Kpuonyer

Reputation: 47

Issue with CGI not returning correct response based on HTML form input

I'm trying to write a simple password system for my website. No username, nothing like that, just a password I can distribute to friends. The idea is to pass a value to the small CGI program written in C below:

#include <stdio.h>
#include <stdlib.h>
int main(){
char *test;
printf("Content-Type: text/html\n\n");            //give the content type
test = getenv("QUERY_STRING");                    //get the data
//printf ("%s\n\n", test);                        //print it for debugging purposes
if (test == "test=test") {                        //check for correct password
    printf("<a href=\"home.html\">Enter.</a>");   //give the link if correct
}
else if (test == NULL){
    printf("Error.");
}
else{
    printf("Denied.", test);
}
return(0);
}

The problem is that, no matter what is passed to it, it always prints "Denied". Whenever I print the returned data, it is exactly what I would expect, i.e. "test=test", yet it still prints "Denied". I'm pretty sure I'm doing this correctly, and I wonder if it is a problem with the server (TinyWeb). The HTML form code is below:

<form action="cgi-bin/cgi.exe" method="get">
    <input name="test" placeholder="Password">
    <input type="submit" value="enter">
</form>

Upvotes: 1

Views: 47

Answers (1)

Amadan
Amadan

Reputation: 198566

In C, you can't compare strings using ==. Use strcmp.

Upvotes: 1

Related Questions