CGI reads the local file must set the file attribute to 666

Question:

I wrote a cgi script in C language to read the string entered from the web page and append the string to a local text file data. txt;

The cgi script is as follows, the source file of collect.c:

#include #include #define MAXLEN 80/* 4 for field name "data", 1 for "=" */#define EXTRA 5/* 1 for added line break , 1 for trailing NUL */#define MAXINPUT MAXLEN+EXTRA+2#define DATAFILE "./data.txt" void unencode(char *src, char *last, char *dest) {for(; src != last; src++ , dest++) if (*src =='+') {*dest = '';} else if(*src =='%') {int code; if (sscanf(src+1, "%2x", &code ) != 1) {code ='?';} *dest = code; src +=2;} else {*dest = *src;} *dest ='
'; *++dest ='' ;} int main(void) {char *lenstr; char input[MAXINPUT]; char data[MAXINPUT]; long len; printf("%s%c%c
", "Content-Ty pe:text/html;charset=iso-8859-1",13,10); printf("Response
"); lenstr = getenv("CONTENT_LENGTH"); if (lenstr == NULL || sscanf(lenstr,"%ld",&len) != 1 || len> MAXLEN) {printf("

Error in invocation-wrong FORM probably. ");} else {printf("< p>CONTENT_LENGTH is %ld.

", len); // fgets(input, len+1, stdin); fgets(input, len+1, stdin); unencode(input+EXTRA, input+ len, data); FILE *fp; fp = fopen(DATAFILE, "a+"); if(fp == NULL) {printf("

Sorry, cannot store your data.

");} else {printf("

Your data is: %s.

", data); fputs(data, fp);} fclose(fp); printf("

Thank you! The following contribution of yours has been stored:

%s ",data);} return 0;}

The web page file is as follows, collect.html:

   
< h3>Please input your words:

Use thttpd as a web server to provide web access services.

The content on the page is shown in the following figure:

The problem is that the string entered into the box can’t be added In the local file data.txt, the error message is as follows:

解决办法:

1. 首先检查脚本collect.c,在命令行中运行Compile the generated collect.cgi:

u1204@u1204-zhw:/usr/ local/www/data/cgi-bin$ ./collect.cgi Content-Type:text/html;charset=iso-8859-1Response

Error in invocation-wrong FORM probably.u1204 @u1204-zhw:/usr/local/www/data/cgi-bin$

Since the input string is empty, so the condition “lenstr == NULL” is established, so it will print out “

Error in invocation-wrong FORM probably.”.

2. Block access to local files in collect.c:

 /// FILE *fp; /// // fgets(input, len+1, stdin); /// /// fgets( input, len, stdin); /// // unencode(input+EXTRA, input+len, data); /// fp = fopen(DATAFILE, "a+"); /// if (fp == NULL) { /// printf("

Sorry, cannot store your data.

"); ///}} /// else {} /// Char tch[] = "How are you! ";} / // fputs(tch, fp); ///} /// fclose(fp);

Recompile to generate collect.cgi, and click the “OK” button on the web page. You can see that there is no longer a “No data received” error message.

It indicates that a certain number of strings have been received.

3. After some toss, the attribute of the local file data.txt is changed from 664 to 666, and then click the “Ok” button on the webpage again, finally The data can be displayed correctly.

Check the local file data.txt, and find that the string entered in the web page is also correctly appended.

Note: The local file data.txt needs to be placed in the cgi-bin directory.

The problem is solved.

Leave a Comment

Your email address will not be published.