Solved

Simple Question

Posted on 2000-04-14
2
142 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
  • 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now