Helpful Information
 
 
Category: Perl/ CGI
Define A Sub-routine Based On Form Variable

I would like to be able to call a subroutine based on meeting an HTTP_REFERER qualification and using input from a select box in a form to define the sub-routine that is called. What I can not figure out, being an embarrassingly uninformed newbie, is how to write the code to replace ($action) in the subroutine call after the condition is met. Can anyone please help me solve this problem?

Here is the form:

<form action="/cgi-bin/gothere.cgi" method="post">
<select name="subroutine">
<option value="null">Select an option...
<option value="null">- - - - - - - - - - - - - - - - -
<option value="Page_1">Main Page
<option value="Page_2">Another Page
<option value="Page_3">That Page
<option value="Page_4">The Other Page

</select>
<input type="submit" value="Go">
</form>


Here is the cgi

#!/usr/bin/perl


$action = $FORM{'subroutine'} ;
if ($action eq 'null')
{$action eq Page_1 ;}


if ($ENV{'HTTP_REFERER'} eq 'abc.com')
{&($action)}

elsif ($ENV{'HTTP_REFERER'} eq 'foo.com')
{&($action)}

elsif ($ENV{'HTTP_REFERER'} eq '123.com')
{&($action)}

else
{&Error_Page}


sub Error_Page

{
print "Content-type: text/html\n\n" ;
print qq~


*******html code here******


~
}


sub Page_1

{
print "Content-type: text/html\n\n" ;
print qq~


*******html code here******


~
}


sub Page_2

{
print "Content-type: text/html\n\n" ;
print qq~


*******html code here******


~
}

i would recomentd doing some if elsif statements to dlcare the correct sub

like:


sub subredir {
if ($action eq "page_1") {&page_1}
elsif ($action eq "page_2) {&page_2}
elsif ($action eq "page_3) {&page_3}
elsif ($action eq "page_4) {&page_4}
}

and then add this subroutine into the if statemets for the referer like this:

if ($ENV{'HTTP_REFERER'} eq 'abc.com' || $ENV{'HTTP_REFERER'} eq 'foo.com' || $ENV{'HTTP_REFERER'} eq '123.com') { &subredir }

else
{&Error_Page}

here i shortended the if statment for the referer so this way, you use less lines and server memory,

that should do whant you want, but still there are better ways, depending what you want, so if you could explain the hole thing, we migh give you a better solution,

if it is like for making a hole site into one script tell me, that the way i work

calilo

Thank you for the solution. To answer your question, only 8 pages of the site will be included in the script.

One more question, if I may .... can I break down the subs into separate files and use a require statement ...

i.e.
require "Page_1.cgi"
require "Page_2.cgi"
etc., etc.

sure you could but it is better to stay with just one script for it all.

what i do for scripts like this, (sites Like this one (http://www.cuernaonline.com/main.cgi) ) is set a simple zone variable $z, and then redirect into the script using subroutines, for each zone in the site.

like
if ($z eq 1) {&main_page}
elsif ($z eq 2) { &surveys}

and so on, just as you are doing it now

calilo

Thank you again for the response. I will take your suggestion and stay with one script.










privacy (GDPR)