Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

Variable-based Redirector..

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
innovator
Asked:
innovator
  • 10
  • 5
  • 2
1 Solution
 
maneshrCommented:
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
 
PeeweeCommented:
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
 
PeeweeCommented:
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
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
innovatorAuthor Commented:
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
 
PeeweeCommented:
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
 
innovatorAuthor Commented:
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
 
maneshrCommented:
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
 
PeeweeCommented:
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
 
PeeweeCommented:
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
 
PeeweeCommented:
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
 
innovatorAuthor Commented:
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
 
PeeweeCommented:
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
 
PeeweeCommented:
innovator,

did you get the answer you were looking for?

regards
Peewee
0
 
innovatorAuthor Commented:
Hi Peewee  ..

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

thanks...
0
 
PeeweeCommented:
innovator,

did you get the solution you were looking for.

regards
Peewee
0
 
innovatorAuthor Commented:
Yes, Peewee  ... it worked great. thanks.
0
 
PeeweeCommented:
innovator,
glad to be of help

regards Peewee
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 10
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now