Solved

Easy modification to script....I HOPE!

Posted on 2001-06-20
14
201 Views
Last Modified: 2013-12-25
Hi all,
I have the following script (included at the botton of this message)

What it basically does is when its called it looks for a variable called "id" and opens an associated text file. For example page.cgi?id=hello
would open hello.txt
It prints this out between a header ($header) and footer ($footer)
What I would like to do is is modify the script to accept the details of which header and footer to display.
For example, if the script was called as page.cgi?id=hello&template=red

Then the header red1.txt and the footer red2.txt would be opened.

Can someone please help me modify the script, and maybe tidy it up a little.

Thanks

Siceman

---------
#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------

open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


 ## Print the footer and exit
 open (FOOTER, "$footer");
 while (<FOOTER>) { print; }
 close(FOOTER);
 exit;
}

open (file, "$name.txt");
 while (<file>) { print; }

open (FOOTER, "$footer");
     while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

   # Get the input
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

   # Split the name-value pairs
   @pairs = split(/&/, $buffer);

   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);

      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

      $FORM{$name} = $value;
   }
}


sub cgi_header {
    print "Content-type: text/html\n";
    print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
    if ($ENV{'REQUEST_METHOD'} eq "POST") {
        read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
    }
    else {
        $incoming = $ENV{'QUERY_STRING'};
    }
    @pairs = split(/&/, $incoming);

    foreach (@pairs) {
        ($name, $value) = split(/=/, $_);

        $name  =~ tr/+/ /;
        $value =~ tr/+/ /;
        $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
        $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

        #### Strip out semicolons unless for special character
        $value =~ s/;/$$/g;
        $value =~ s/&(\S{1,6})$$/&\1;/g;
        $value =~ s/$$/ /g;

        $value =~ s/\|/ /g;
        $value =~ s/^!/ /g; ## Allow exclamation points in sentences

        #### Skip blank text entry fields
        next if ($value eq "");

        #### Check for "assign-dynamic" field names
        #### Mainly for on-the-fly input names, especially checkboxes
        if ($name =~ /^assign-dynamic/) {
            $name = $value;
            $value = "on";
        }

     #### Allow for multiple values of a single name
        $FORM{$name} .= ", " if ($FORM{$name});

        $FORM{$name} .= $value;
    }
}
0
Comment
Question by:siceman105
  • 9
  • 5
14 Comments
 
LVL 8

Expert Comment

by:bebonham
ID: 6212461
why are you in the cgi area and not using cgi.pm :)

really, in all seriousness...it is a standard part of perl, and I would be glad to decomplicate your script by using cgi.pm if that is okay, and I can also add that dynamic headers...

since you already are using lwp, why not use cgi?

Bob
0
 

Author Comment

by:siceman105
ID: 6212468
Im not sure how to use cgi.pm

But Im open to it if youd show me what to do etc!

Cheers

Siceman
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6212475
how can this work ??!?!

$name = $FORM{id}; ??

doesn't it have to be $FORM{'id'} or $FORM{$id} !?!?

I would like an explanation on that one...

anyways, try these changes.

it should work...I can't belive that id thing works...

just make sure you make a field in your html form called template!!





#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'}
$footer=$header + "2";
$header=$header + "1";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
    while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

  # Get the input
  read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

  # Split the name-value pairs
  @pairs = split(/&/, $buffer);

  foreach $pair (@pairs) {
     ($name, $value) = split(/=/, $pair);

     $value =~ tr/+/ /;
     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

     $FORM{$name} = $value;
  }
}


sub cgi_header {
   print "Content-type: text/html\n";
   print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
   if ($ENV{'REQUEST_METHOD'} eq "POST") {
       read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
   }
   else {
       $incoming = $ENV{'QUERY_STRING'};
   }
   @pairs = split(/&/, $incoming);

   foreach (@pairs) {
       ($name, $value) = split(/=/, $_);

       $name  =~ tr/+/ /;
       $value =~ tr/+/ /;
       $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
       $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

       #### Strip out semicolons unless for special character
       $value =~ s/;/$$/g;
       $value =~ s/&(\S{1,6})$$/&\1;/g;
       $value =~ s/$$/ /g;

       $value =~ s/\|/ /g;
       $value =~ s/^!/ /g; ## Allow exclamation points in sentences

       #### Skip blank text entry fields
       next if ($value eq "");

       #### Check for "assign-dynamic" field names
       #### Mainly for on-the-fly input names, especially checkboxes
       if ($name =~ /^assign-dynamic/) {
           $name = $value;
           $value = "on";
       }

    #### Allow for multiple values of a single name
       $FORM{$name} .= ", " if ($FORM{$name});

       $FORM{$name} .= $value;
   }
}



good luck,

Bob
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 8

Expert Comment

by:bebonham
ID: 6212500
cool...this is in cgi...

because it looks simpler :)


#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;
use CGI ':standard';

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'}
$footer=$header + "2";
$header=$header + "1";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
    while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {
&cgi_receive;
}



sub cgi_header {
 print header unless ($FORM{'next-url'});
}        

sub cgi_receive {

foreach(param())
{
$FORM{$_}=param($_);
}


otherwise, I pretty much like the script...

I like how you have an error routine that include the printing of the header...

I would have done it like this
..this isn't real code...this is just the logic I would have used...

if(error)
{
print (errmsg);
}

if(!error)
{
print (correcttextFile);
}

print footer;
exit;

but you did...

if(error)
{
print (errmsg);
print header;
exit;
}

print (correcttextFile);
print footer;
exit;


I think I like your logic better :)


Bob
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6212514
hold on here...I will repost...

forgot to add the .txt to the new files...


first, the CGI version:


#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;
use CGI ':standard';

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'}
$footer=$header + "2.txt";
$header=$header + "1.txt";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
   while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {
&cgi_receive;
}



sub cgi_header {
print header unless ($FORM{'next-url'});
}        

sub cgi_receive {

foreach(param())
{
$FORM{$_}=param($_);
}





#######################
okay, now for the non-cgi version:







#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'}
$footer=$header + "2.txt";
$header=$header + "1.txt";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
   while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

 # Get the input
 read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

 # Split the name-value pairs
 @pairs = split(/&/, $buffer);

 foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);

    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    $FORM{$name} = $value;
 }
}


sub cgi_header {
  print "Content-type: text/html\n";
  print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
  if ($ENV{'REQUEST_METHOD'} eq "POST") {
      read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
  }
  else {
      $incoming = $ENV{'QUERY_STRING'};
  }
  @pairs = split(/&/, $incoming);

  foreach (@pairs) {
      ($name, $value) = split(/=/, $_);

      $name  =~ tr/+/ /;
      $value =~ tr/+/ /;
      $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
      $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

      #### Strip out semicolons unless for special character
      $value =~ s/;/$$/g;
      $value =~ s/&(\S{1,6})$$/&\1;/g;
      $value =~ s/$$/ /g;

      $value =~ s/\|/ /g;
      $value =~ s/^!/ /g; ## Allow exclamation points in sentences

      #### Skip blank text entry fields
      next if ($value eq "");

      #### Check for "assign-dynamic" field names
      #### Mainly for on-the-fly input names, especially checkboxes
      if ($name =~ /^assign-dynamic/) {
          $name = $value;
          $value = "on";
      }

   #### Allow for multiple values of a single name
      $FORM{$name} .= ", " if ($FORM{$name});

      $FORM{$name} .= $value;
  }
}




okay,


there it is, all fixed up.

Bob
0
 

Author Comment

by:siceman105
ID: 6212534
So the first one uses CGI.pm? Does that mean it will run faster or something?

Thanks

Siceman
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6212545
no it actually means it will run slower :)
really!

cgi.pm is one of the biggest (besides lwp and gd) best modules available for perl...(it also takes a few seconds...maybe milliseconds) to load

the code without the cgi.pm will run faster, and be more portable...

of course...cgi.pm is great, and it's nice to have it available to your scripts...I personally always use cgi.pm if I am going to be reading fields from an http post...

but that is a matter of preference...

totally up to you...

maneshr, #1 in both this area and the perl area, would probably suggest you use the NON cgi version.

Bob
0
 

Author Comment

by:siceman105
ID: 6213486
Hi,
yeah Manshr helped me develop the first one.

However, the non cgi version you just gave me doesnt seem to work, I get an Error 500 with the following details:

Scalar found where operator expected at /home//temp.cgi line 22, at end of line
     (Missing semicolon on previous line?)
syntax error at /home//temp.cgi line 22, near "$footer"
Execution of /home//temp.cgi aborted due to compilation errors.



Any ideas?

Thanks

Steve
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6214185
man...
sometimes I wonder about myself :)

I can't even type 2 lines of code without a typo!

this is the problem in BOTH scripts...

$header=$FORM{'template'}


needs to have a ; after it so this is the fixed non-cgi version.



#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'};
$footer=$header + "2.txt";
$header=$header + "1.txt";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
  while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);

   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

   $FORM{$name} = $value;
}
}


sub cgi_header {
 print "Content-type: text/html\n";
 print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
 if ($ENV{'REQUEST_METHOD'} eq "POST") {
     read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
 }
 else {
     $incoming = $ENV{'QUERY_STRING'};
 }
 @pairs = split(/&/, $incoming);

 foreach (@pairs) {
     ($name, $value) = split(/=/, $_);

     $name  =~ tr/+/ /;
     $value =~ tr/+/ /;
     $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
     $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

     #### Strip out semicolons unless for special character
     $value =~ s/;/$$/g;
     $value =~ s/&(\S{1,6})$$/&\1;/g;
     $value =~ s/$$/ /g;

     $value =~ s/\|/ /g;
     $value =~ s/^!/ /g; ## Allow exclamation points in sentences

     #### Skip blank text entry fields
     next if ($value eq "");

     #### Check for "assign-dynamic" field names
     #### Mainly for on-the-fly input names, especially checkboxes
     if ($name =~ /^assign-dynamic/) {
         $name = $value;
         $value = "on";
     }

  #### Allow for multiple values of a single name
     $FORM{$name} .= ", " if ($FORM{$name});

     $FORM{$name} .= $value;
 }
}



okay, hope that works for you,

Bob

0
 

Author Comment

by:siceman105
ID: 6215153
Hi,
thanks. Im not getting the error any more, but the header and footer arent appearing as expected. Im wondering if it has something to do with this bit of code

$header=$FORM{'template'};
$footer=$header + "2.txt";
$header=$header + "1.txt";

isnt $header being defined twice?

Thanks

Siceman
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6216530
doesn't matter, I am reassigning the value...

but... in this case, the value that is being assigned is not existing.

and in this case, it is again an error of my poor debugging...


I am doing this $string + "string";

which is okay in javascript, but in perl, it doesn't work...you have to use .

so...here is the once again corrected code...sorry about the trouble :)


#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$header=$FORM{'template'};
$footer=$header . "2.txt";
$header=$header . "1.txt";
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
 while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
  ($name, $value) = split(/=/, $pair);

  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

  $FORM{$name} = $value;
}
}


sub cgi_header {
print "Content-type: text/html\n";
print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
if ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
}
else {
    $incoming = $ENV{'QUERY_STRING'};
}
@pairs = split(/&/, $incoming);

foreach (@pairs) {
    ($name, $value) = split(/=/, $_);

    $name  =~ tr/+/ /;
    $value =~ tr/+/ /;
    $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
    $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

    #### Strip out semicolons unless for special character
    $value =~ s/;/$$/g;
    $value =~ s/&(\S{1,6})$$/&\1;/g;
    $value =~ s/$$/ /g;

    $value =~ s/\|/ /g;
    $value =~ s/^!/ /g; ## Allow exclamation points in sentences

    #### Skip blank text entry fields
    next if ($value eq "");

    #### Check for "assign-dynamic" field names
    #### Mainly for on-the-fly input names, especially checkboxes
    if ($name =~ /^assign-dynamic/) {
        $name = $value;
        $value = "on";
    }

 #### Allow for multiple values of a single name
    $FORM{$name} .= ", " if ($FORM{$name});

    $FORM{$name} .= $value;
}
}


okay, good luck!

Bob
0
 

Author Comment

by:siceman105
ID: 6216557
Cheers Bob! It works!

Just one last thing, is there any way that a small bit of code could be added setting a default for the template incase no variable or an invalid one is entered

Siceman
0
 
LVL 8

Accepted Solution

by:
bebonham earned 150 total points
ID: 6216812
certainly,

I'm glad it is working...



#!/usr/local/bin/perl
#

use LWP::Simple;
use LWP::UserAgent;

&cgi_header;
&cgi_receive;

# Here are your variables, change them to your liking

$header = "/data1/hypermart.net/x/header.html";                    # The location of your header file
$footer = "/data1/hypermart.net/x/footer.html";                    # The location of your footer file
$script_url = "/page.cgi";                    # URL of this program
$horoscopes_dir = "/data1/hypermart.net/x/weekly.txt";     # The locations of your weekly.txt file

# Shouldn't have to change anything below this
#------------------------------------------------------------------------------------------------


$template=$FORM{'template'};
$footer= -e $template . "2.txt" ? $template . "2.txt" : $footer;
$header= -e $template . "1.txt" ? $template . "1.txt" : $header;
open (header, "$header");
while (<header>) { print; }
$name = $FORM{id};
if ($name eq "") { $name="error"; }

#open (HEADER, "$header");
#     while (<HEADER>) { print; }

if (!(-f  "$name.txt") ){ ## That file DOES NOT exist!!

## Open a custom error page
$name="error";
open(E,"$name.txt") || die $!;
@E=<E>;
close(E);

print join("",@E);


## Print the footer and exit
open (FOOTER, "$footer");
while (<FOOTER>) { print; }
close(FOOTER);
exit;
}

open (file, "$name.txt");
while (<file>) { print; }

open (FOOTER, "$footer");
while (<FOOTER>) { print; }

#<-------===========Subroutines===========------->
#
# cgi_header      { Outputs HTML Header }
# cgi_receive     { Decodes POST-type Forms }
# parse_form     { Decodes GET-type Forms }
#

sub parse_form {

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
 ($name, $value) = split(/=/, $pair);

 $value =~ tr/+/ /;
 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

 $FORM{$name} = $value;
}
}


sub cgi_header {
print "Content-type: text/html\n";
print "\n" unless ($FORM{'next-url'});
}        

sub cgi_receive {
if ($ENV{'REQUEST_METHOD'} eq "POST") {
   read(STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
}
else {
   $incoming = $ENV{'QUERY_STRING'};
}
@pairs = split(/&/, $incoming);

foreach (@pairs) {
   ($name, $value) = split(/=/, $_);

   $name  =~ tr/+/ /;
   $value =~ tr/+/ /;
   $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
   $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

   #### Strip out semicolons unless for special character
   $value =~ s/;/$$/g;
   $value =~ s/&(\S{1,6})$$/&\1;/g;
   $value =~ s/$$/ /g;

   $value =~ s/\|/ /g;
   $value =~ s/^!/ /g; ## Allow exclamation points in sentences

   #### Skip blank text entry fields
   next if ($value eq "");

   #### Check for "assign-dynamic" field names
   #### Mainly for on-the-fly input names, especially checkboxes
   if ($name =~ /^assign-dynamic/) {
       $name = $value;
       $value = "on";
   }

#### Allow for multiple values of a single name
   $FORM{$name} .= ", " if ($FORM{$name});

   $FORM{$name} .= $value;
}
}

the default value for $header and $footer as set in the begenning of the script will be used if the entered header/footer don't exist.

and good news, I tested it and it works!


hope that helps,

Bob
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6229445
did you have a chance to try the above modified script?
take your time, though, if you need to.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sed question 2 103
Batch FIle delete files with particular date 10 99
Configure Robocopy to excluding folders 6 63
How can i open a .fxp file 3 40
Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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