Guildencrantz has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I'm working on a database using a dbm file indexed with the IP of the user's computer ($ENV{REMOTE_ADDR}). The problem is that if I set it to $dbm_hash{$ENV{REMOTE_ADDR}} I get errors, both in CLI and CGI modes. If I do $userIP = $ENV{REMOTE_ADDR} and then call $dbm_hash($userIP) the same problems occure. However, if I do $userIP = "192.1.1.191" the script will run in CLI, but still errors out in CGI. What am I doing wrong?

~~Guildencrantz
#my $userIP = $ENV{REMOTE_ADDR}; my $userIP = "129.168.1.100"; my @formData = split/&/, $ENV{QUERY_STRING}; my %rawData; my $dbEntry; foreach (@formData) { my ($name, $value) = split /=/, $_; $name =~ tr/+/ /; $name =~ s/%([a-f0-9][a-f0-9])/chr(hex($1))/egi; $value =~ tr/+/ /; $value =~ s/%([a-f0-9][a-f0-9])/chr(hex($1))/egi; $rawData{$name} = $value; } $dbEntry = "<TR><TD>$userIP</TD><TD>$rawData{user}</TD><TD>$rawData{se +rver}</TD><TD>$rawData{serverpw}</TD><TD>$rawData{gameDiff}</TD><TD>$ +rawData{worldDiff}</TD><TD>$rawData{map}</TD><TD>$rawData{dod}</TD><T +D>$rawData{teamEnabled}</TD><TD>$rawData{pauseEnabled}</TD><TD>$rawDa +ta{pvpEnabled}</TD><TD>$rawData{joinEnabled}</TD><TD>$rawData{chooseS +tartEnabled}</TD><TD>$rawData{newRestrictionEnabled}</TD></TR>"; my %allIPs; dbmopen(%allIPs, "database/allIPs", 0664) or die "Cannot open allIPs database: $!"; $allIPs{$userIP} = $dbEntry or die "$!"; dbmclose %allIPs;

Replies are listed 'Best First'.
Re: dbm troubles
by tachyon (Chancellor) on Apr 12, 2002 at 16:35 UTC

    If you have a problem please post the actual error messages, they really help!

    What am I doing wrong?

    1) Hand parsing your form data. use CGI or die; You could replace all your initial code with:

    use CGI; use CGI::Carp 'fatalsToBrowser'; # this will make debugging easier my $q = new CGI; my $userIP = $q->remote_host; # get REMOTE_HOST my %rawData = $q->Vars(); # get CGI data as a hash

    2) When you do your dbmopen you should have something like this (a)full path to file and (b) possibly the 0666 option. Note at the CL you script will run with your permissions so 0600 on the allIPs db file should be OK. Under CGI it will run as user 'nobody' so you need R/W for everybody so chmod 666 allIPs (606 should also do)

    my %allIPs; dbmopen( %allIPs, "/full/path/to/database/allIPs", 0666 ) or die "Cannot open allIPs database: $!"; $allIPs{$userIP} = $dbEntry or die "$!"; dbmclose %allIPs;

    3) Not reporting or perhaps even looking for error messages. With a CGI you often need to look in the server logs or write a die_nice() function. CGI Help Guide

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print