Solved

Simple Question

Posted on 2000-04-14
2
151 Views
Last Modified: 2013-12-25
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
Comment
Question by:MegaX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Accepted Solution

by:
venky75 earned 73 total points
ID: 2718450
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
 

Expert Comment

by:venky75
ID: 2718452
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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question