Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

Simple Question

how do i write a cgi that decodes entry from a form and then saves it to a file? (i don't have cgi.pm so is this possible?)
0
MegaX
Asked:
MegaX
  • 2
1 Solution
 
venky75Commented:
Hi,

try this program out it.Doesnot require any .pm files for decoding the entries and also saves to file.

bye
venky

#!/usr/bin/perl

%in= &getcgivars ;

open(FILE,">>filename.txt");
print FILE "\n";
$maxlength= 0 ;
foreach (keys %in)
{
 $maxlength= length if length > $maxlength ;
}
$maxlength++ ;
foreach (sort keys %in)
{
 if ($in{$_}=~ /\n/)
 {
  $in{$_}= "\n" . $in{$_} ;
  $in{$_}=~ s/\n/\n    /g ;
  $in{$_}.= "\n" ;
 }
 $in{$_}=~ s/\0/, /g ;
 printf FILE "%-${maxlength}s  %s\n", "$_:", $in{$_} ;
}
close (BOOK);

sub getcgivars
{
 local($in, %in) ;
 local($name, $value) ;
 if ($ENV{'REQUEST_METHOD'} eq 'GET')
 {
  $in= $ENV{'QUERY_STRING'} ;
 }
 elsif ($ENV{'REQUEST_METHOD'} eq 'POST')
 {
  if ($ENV{'CONTENT_TYPE'}=~ m#^application/x-www-form-urlencoded$#i)
  {
   $ENV{'CONTENT_LENGTH'} || &HTMLdie("No Content-Length sent with the POST request.") ;
   read(STDIN, $in, $ENV{'CONTENT_LENGTH'}) ;
   }
   else
   {
    &HTMLdie("Unsupported Content-Type: $ENV{'CONTENT_TYPE'}") ;
   }
 }
 else
 {
  &HTMLdie("Script was called with unsupported REQUEST_METHOD.") ;
 }
   
 foreach (split('&', $in))
 {
  s/\+/ /g ;
  ($name, $value)= split('=', $_, 2) ;
  $name=~ s/%(..)/chr(hex($1))/ge ;
  $value=~ s/%(..)/chr(hex($1))/ge ;
  $in{$name}.= "\0" if defined($in{$name}) ;  
  $in{$name}.= $value ;
 }
 return %in ;
}

sub HTMLdie
{
 local($msg,$title)= @_ ;
 $title || ($title= "CGI Error") ;
 print <<EOF ;
 Content-type: text/html

 <html>
 <head>
 <title>$title</title>
 </head>
 <body>
 <h1>$title</h1>
 <h3>$msg</h3>
 </body>
 </html>
 EOF
 exit ;
}
0
 
venky75Commented:
Hi there,
 sorry about the part where it is close(BOOK); instead it should be close(FILE);

Kindly change the same at ur end.

Bye
venky
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now