Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-14-2002, 03:41 AM   PM User | #1
Myagi
New to the CF scene

 
Join Date: Jun 2002
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Myagi is an unknown quantity at this point
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******


~
}

Last edited by Myagi; 07-14-2002 at 04:00 AM..
Myagi is offline   Reply With Quote
Old 07-14-2002, 04:11 AM   PM User | #2
Calilo
Regular Coder

 
Join Date: Jun 2002
Location: Mex
Posts: 122
Thanks: 0
Thanked 0 Times in 0 Posts
Calilo is an unknown quantity at this point
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
Calilo is offline   Reply With Quote
Old 07-14-2002, 05:14 AM   PM User | #3
Myagi
New to the CF scene

 
Join Date: Jun 2002
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Myagi is an unknown quantity at this point
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.
Myagi is offline   Reply With Quote
Old 07-14-2002, 05:33 AM   PM User | #4
Calilo
Regular Coder

 
Join Date: Jun 2002
Location: Mex
Posts: 122
Thanks: 0
Thanked 0 Times in 0 Posts
Calilo is an unknown quantity at this point
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 ) 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
Calilo is offline   Reply With Quote
Old 07-14-2002, 09:27 PM   PM User | #5
Myagi
New to the CF scene

 
Join Date: Jun 2002
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Myagi is an unknown quantity at this point
Thank you again for the response. I will take your suggestion and stay with one script.
Myagi is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:14 AM.


Advertisement
Log in to turn off these ads.