Can't create file in CGI Script

Hi I have some simple script within the context of a cgi script that create a file to write to. If I run the script from the command line as root the file is created with no issue. However when I run the script from a web page with the apache account, it dies. The code lines are:

sub Populate_Session_File       {
        open (NEWOUT,">/var/www/cgi-bin/sessions/$SID") || die "can't open the sessions file";
                print NEWOUT "$SID\011$empnum\011$first_name\011$last_name\011$mailadder\n";
        close (NEWOUT) || die "can't close the spaceout file";
} # end populate session File

And the error I get is:

"[Tue Mar 12 06:07:58 2013] [error] [client IP] can't open the sessions file at /var/www/cgi-bin/riskpect_dsp_login.pl line 238., referer: http://blah.blah
[Tue Mar 12 06:07:58 2013] [error] [client IP] Premature end of script headers: riskpect_dsp_login.pl, referer: http://blah.blah
"

Thanks in advance for any help

httpserver user has to be write priviledge to the destination dir. Something like example:

# give tmp dir priviledge - everyone can create own file, but only owner can remove it. Compare ex. /var/tmp priviledges
# sticky bit = restricted deletion
chmod 1777 /var/www/cgi-bin/sessions

Or

If ex. www-data is the apache http server user then something

chown www-data /var/www/cgi-bin/sessions
chmod 755 /var/www/cgi-bin/sessions
# or
chmod 1755 /var/www/cgi-bin/sessions

kshji, I attempted to send you a personal message to thank you. Your advice was most helpful, and I'm back on track!!! For the Record...

YOU CHAMPION!!!

Cheers

If our paths ever cross the drinks are on me!

Larry

When you get an error, I bet PERL has something like C perror() to explicitly say what the error is !