Problem to write CGI variable to a log file

Hi everyone! I'm new here:)

I'm trying to write just received CGI variable to a log file. CGI script doesn't create this file. I run the script by clicking hyperlink from another web address. Please check this simple code, why it doesn't work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib> 

using std::cout;
using std::endl;
using std::string;
using std::ofstream;

int main() 
{
    cout << "Content-type: text/plain" << endl << endl;

    char* referer = getenv("HTTP_REFERER");

    if (referer != nullptr)
    {
        string pathLog = "/var/www/cgi-bin/reflog.txt";
        ofstream writeLog;

        writeLog.open(pathLog, ofstream::app);
        writeLog << referer << "\n";
        writeLog.close();

    }


    return 0;
}
Welcome. I have a couple guesses, but I'm pretty sure it's the second one:
1. Does referer actually get anything when you call getenv()?
->Use cout to output it after the function is called.
2. Do you have read/write permisions on /var/www/cgi-bin folder?
-> Do you know how to use chmod/chown? Look up creating a group and adding your user to that group. That, or change pathLog to output to a local folder that your user owns by default...
Last edited on
Thank you newbieg for your answer.
1. Yeah, "cout" works fine. I have tested it right in two places:
inside:
writeLog << referer << "\n";
cout << referer;
writeLog.close();

and after:
}
cout << referer;
return 0;
}

both ways show the referer on the cgi-page web address.

2. There is command: "chmod 755 -R /var/www/cgi-bin/" - it must give read/write permission for whole directory. Also I'm testing it under root access.

Still no one log file there(
OK. So the environment variable is good. You're under root access, so permissions should not be an issue (though it's still possible).

1. Try checking if the file is open and ready to write to:
1
2
3
4
5
6
7
8
if(writeLog.good())
{
    //write to file
}
else
{
    cout << "File did not open correctly\n";
}


2. Does the file itself exist now that you've tried to write to it, or is there no file at this point?


3. Try an existing program to write to that file path echo "hello world" >> /var/www/cgi-bin/reflogTest.txt And see if that file is created properly. (This would confirm file permissions are good, and that it's this code causing issues)

4. Is this on your local machine, or are you ssh'd in?

5. If none of this is productive, then edit your original post; in the upper area near the post's title there's a drop-down menu for which forum this is posted in. Move it to the Unix/Linux forum so we get a more focused perspective.
Last edited on
1. I have checked two ways: if (writeLog.good()).... and if (!writeLog.is_open())...... the result the same - File did not open correctly.

2. There is no file.

3. Works fine - a new file was created with the text inside. It seems file permission is fine. Nice command, thank you!)

4. Remote ssh access.

5. Looks like something was productive))

Maybe I need to find some other function - for example - write()? I hope I don't need to change encoding to translate this variable content to write it in the file.
Last edited on
Problem is solved. My fault was an unavailability of the log file with access permission "chmod 666" on it. I thought command "chmod 755" on "cgi-bin" folder will provide creation of a new file. It works when I run the cgi file under terminal, but does not work if I run cgi file from web browser. I have created the file (command: touch filename) and set "chmod 666" on it. It works now.
Topic archived. No new replies allowed.