Figure 1/Web Developer® Vol. 2 No. 3

Figure 1

Perl code for cookie processing

Sample URL to reach the simple output script in the NPH script sidebar:



<a href="/cgi-bin/nph-cookie-form?html=%2Fforms%2baby.pix.html">Form</a>

---perl to check on cookie and restrict to one-per ---

# assumes that %in is filled with contents of HTML-passed form variables 

# and that the variable 'out' contains the output file name sent by the HTML form

    $cookie = $ENV {'HTTP_COOKIE'};

    if (!$cookie) {$cookie = $ENV {'REMOTE_ADDR'} . "." .

$ENV {'HTTP_USER_AGENT'};

    open ( IN, "< $in {'out'}" );

    while ( <IN> ) {

    if (/$cookie/) {$foundcookie = 1; [advance to EOF?];

    }

    close IN;

    if ($foundcookie) {

    print "I'm sorry, but our records show you've already filled out this form. We only allow one entry per browser per week. Please e-mail the administrators if you think you got this message in error. \n";

    exit;

    }

    open (OUT, ">> $in ['out']");

    foreach (@newrecord)  {

    print OUT . "\t";

    }

    print OUT . "\t$cookie\n";

    close OUT;

--- end perl fragment ---