Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Variable-based Redirector..

Posted on 2002-04-04
17
Medium Priority
?
262 Views
Last Modified: 2013-12-25
Hi..

Can anyone refer me to a simple redirector program/script?

I am searching for a software that redirects user acoording to
variables in the URL

FOR EXAMPLE:
------------

clicking on website.com/redirector.cgi?variable1
will send user to yahoo.com (for example).

While clicking on website.com/redirector.cgi?variable2
will send user to hotmail.com (just an example)

and so on... In the "redirector.cgi"
i'll set the variables and to where it should
redirect the users.

Can anyone tell me where to get such a code?


Thanks.
0
Comment
Question by:innovator
[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
  • 10
  • 5
  • 2
17 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 6919864
innovator,

".. Can anyone tell me where to get such a code?.."

Do you have any particular CGI scripting language in mind?

Are you fine with using Perl? If so, then i can write a custom script per your requirements.

Let me know.
0
 
LVL 5

Expert Comment

by:Peewee
ID: 6920384
innovator,

redirection in Perl is done with the print "Location: url\n\n"; statement.

a simple example would be:
print "Location: http://www.experts-exchange.com\n\n";

a more complex example would be:
print "Location: /cgi-bin/view.pl?op=l&person=$person&client=$client&image=$image&a=s\n\n";

where variables are passed in a form and parsed into the perl script with a name value usuage.  This is how i believe you wanted to use the redirect.

do you need more help on any issues i have raised here.

regards
Peewee
0
 
LVL 5

Expert Comment

by:Peewee
ID: 6920387
innovator,

for off the shelf scripts ready for this purpose look no further than:
 http://cgi.resourceindex.com/Programs_and_Scripts/Perl/Redirection/

hope this helps
regards Peewee
0
Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

 

Author Comment

by:innovator
ID: 6920629
maneshr... Yes... Perl  if very fine wih me.

Peewee  ..I already had a look at http://cgi.resourceindex.com/Programs_and_Scripts/Perl/Redirection/
with no luck!

none of these programs directs user according to the variable in the URL itself...

they redirect according REMOTE_HOST environment variable
0
 
LVL 5

Expert Comment

by:Peewee
ID: 6920729
innovator,

are you able to understaand and code one based on variables with the print Location?

Here's an example.  The scripts will expect two form values
named site_one and site_two.  These are set to a value other than spaces when they are intended to redirect to.  The scripts checks to see if any of these exist if they do they redirect to a given user page.  

my $q = use CGI;
$site1 = $q->param('site_one');
$site2 = $q->param('site_two');

if ($site1)
{
    $url = 'http://www.hotmail.com';
}
elsif ($site2)
{
    $url = 'http://www.experts-exchange.com';
}


print "Location: $url\n\n";


Is this closer to what you need?

regards
Peewee
0
 

Author Comment

by:innovator
ID: 6920903
Ok Peewee ..

I am a bit new to perl.. so, what you wrote seem very ok with me..

but here are some few notes:
1) Shouldn't perl programs start with somthing like
#!/usr/bin/perl    ?

2) print "Location: $url\n\n";  
does it mean that the visitor will *see* the URL on screen?
or he/she will be *sent* to that URL?

3) the variable should look like: redirect.cgi?site_one
OR redirect.cgi?site1

4) Can you please add a very simple (and basic) log file?
for example  LOG.TXT file that contains the following info:
site_one  5
site_two  9

(which means that variable one, was visited 5 times,
while variable 2 was visited 9 times).

I am sorry, i forgot to mention the log file in my question.
Just remembered it when i saw the code not having a log file.

5) Then, Can you please post the full code..
i will simply copy and paste it (as it is) into a txt
file, rename it to .cgi , chmod to 755 then test.

Thank you very much.

0
 
LVL 16

Expert Comment

by:maneshr
ID: 6921150
innovator,

"..maneshr... Yes... Perl  if very fine wih me..."

Excellent!!! Since you are already being helped by Peewee, i will let you two figure out a solution. In the event you do not get a satisfactory solution, i will step in and work on the solution for you.
0
 
LVL 5

Accepted Solution

by:
Peewee earned 1196 total points
ID: 6928869
innovator,

here a perl program i've written simply copy and paste it.

it expects a text file called redirect.txt and example is directly below - the key is id, url location and count.

You will need a html form which poinys at the perl script and expects one from variable with a name of redirect.

This scripts will redirect you to a location defined in the text file and keeps a count of the times a referral has been made.

let me know if yoiu have nay problems.

regards
Peewee

=====================================
1,www.hotmail.com,1
2,http://www.sky.com,0
3,www.experts-exchange.com,0
4,www.bbc.co.uk,0
5,http://www.avfc.co.uk,3

==================================
#!/usr/bin/perl

use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q =  new CGI;

$form_redirect = '1';
$form_redirect = $q->param('redirect') || die "no redirect has been passed from the form";

my $location;

### read access
open (DB,"<redirect.txt") || die "cant open DB:$!";
@records = <DB>;
close (DB);


###write access
open (FILE,">redirect.txt") || die "cant open DB:$!";

foreach my $record (@records)
{
     chomp $record;
     my ($db_id, $db_site, $db_counter) = split (/,/,$record);
     
     if ($form_redirect == $db_id)
     {
          ### add counter and redirect browser
          $db_counter++;
          $location = $db_site;
          print FILE "$db_id,$db_site,$db_counter\n";
     }
     else
     {
          ### rewrite contents back to the file
          print FILE "$db_id,$db_site,$db_counter\n";

     }

}


close (DB);
print "Location: $location\n\n";



0
 
LVL 5

Expert Comment

by:Peewee
ID: 6928876
just to make this clear this in not part of the perl script it is an example text file i used, note they have to be delimetered with comma's, ie comma seperated:

1,www.hotmail.com,1
2,http://www.sky.com,0
3,www.experts-exchange.com,0
4,www.bbc.co.uk,0
5,http://www.avfc.co.uk,3

regards
Peewee
0
 
LVL 5

Expert Comment

by:Peewee
ID: 6928935
innovator,
also one point i should clarify is that the value in the html form variable should match the first field of the text file, ie in my example 1-5.

ie an example form variable would be:

<input type="hidden" name="redirect" value="1">
therefore on submission of our hypothethical form we would be redirected the the website: www.hotmail.com

regards
Peewee
0
 

Author Comment

by:innovator
ID: 6929638
Hi Peewee  ..

Thanks for your code.. but there is somthing that i couldn't understand?


In my question, i said that the redirection will be based on the variable IN the browser's location bar:
website.com/redirector.cgi?variable2

but it semms that your code works only if someone click on a submit button (form submittion) ?

is that true or it can be working directly from the browser
(like i mentioned in my main question)

thank you.

0
 
LVL 5

Expert Comment

by:Peewee
ID: 6930858
innovator,
you will need to pass to redirect.cgi a variable named redirect with a value of between 1 - 5 in myexample.

hence:

redirect.cgi?redirect=1 would be fine.

or a form submission would also be fine.

any problems let me know..
regards
peewee

0
 
LVL 5

Expert Comment

by:Peewee
ID: 6941486
innovator,

did you get the answer you were looking for?

regards
Peewee
0
 

Author Comment

by:innovator
ID: 6944277
Hi Peewee  ..

My server was down, i couldn't test it.. will do so and back
to you today or tomorrow.

thanks...
0
 
LVL 5

Expert Comment

by:Peewee
ID: 6947489
innovator,

did you get the solution you were looking for.

regards
Peewee
0
 

Author Comment

by:innovator
ID: 6953669
Yes, Peewee  ... it worked great. thanks.
0
 
LVL 5

Expert Comment

by:Peewee
ID: 6954037
innovator,
glad to be of help

regards Peewee
0

Featured Post

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

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

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…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The viewer will learn how to count occurrences of each item in an array.
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 …

715 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