mishko has asked for the wisdom of the Perl Monks concerning the following question:

hello, i'm working on an ssi call to a cgi script that would detect the referring url, and if it's a certain offending domain then it would redirect that viewer to another site. i'm having little luck with what code i've tried to slap together so any help would be much appreciated....

#!/usr/bin/perl print "Content-type: text/html\n\n"; #if the refering agent is dumbdomain.com if ( $ENV{HTTP_REFERER} == !~ m|^http://dumbdomain.com/| ) #then redirect to anotherpage.com { print "Location: http://www.anotherpage.com\n\n"; }
thanks in advance everyone!

Edit 2001-05-14 by mirod: added code tags

Replies are listed 'Best First'.
Re: blocking naughty referrers
by merlyn (Sage) on May 14, 2001 at 10:14 UTC
    Be aware that any scheme involving REFERER can be subverted, as it is an unverified header given by the browser. Programs using LWP as a Perl client can set arbitrary referers for each request, for example.

    -- Randal L. Schwartz, Perl hacker

      However, you can use $ENV{REMOTE_ADDR} (the ip of the connecting client). This cannot be spoofed (although they could go and use a proxy).

      my @a=qw(random brilliant braindead); print $a[rand(@a)];
(dkubb) Re: (2) blocking naughty referrers
by dkubb (Deacon) on May 14, 2001 at 10:21 UTC

    The main reason your code doesn't work, is that you have sent two \n's to the browser, before you attempt to redirect it. Once you have sent the double carriage returns, it's game over, anything printed to the browser after that is considered content. That is, as long as you've specified the Content-Type at some point before the carriage returns.

    The key is to redirect the browser, if you need to, before printing the Content-Type, and sending the double carriage returns.

    The follow code snippet will allow you to specify a list of bad domains, and redirects the user back to the bad domain if a match is found:

    #!/usr/bin/perl -wT use strict; use CGI; use URI; use constant BAD_DOMAINS => qw( www.baddomain.com ); #create lookup table my %bad_domains; undef @bad_domains{ BAD_DOMAINS() }; my $cgi = CGI->new; print $cgi->redirect($cgi->referer) if exists $bad_domains{ URI->new($cgi->referer, 'http')->host }; print $cgi->header; #...
Re: blocking naughty referrers
by snafu (Chaplain) on May 14, 2001 at 09:41 UTC
    try something like:

    if ( $ENV{'HTTP_REFERRER'} =~ /dumbdomain.com/ ) { print("Location: http://www.anotherpage.com\n\n"); }
    I believe this will work...although, it is untested.

    ----------
    - Jim

      Very close, except that you spelled "referrer" correctly, and HTTP doesn't. Fleshed out a bit more, this looks like
      #!/usr/bin/perl if ( defined $ENV{'HTTP_REFERER'} and $ENV{'HTTP_REFERER'} =~ /dumdomain\.com/ ) { print "Location: http://www.anotherpage.com\n\n"; exit(0); } print "Content-type: text/html\n\n"; ...
        Ahh, thank you! I also noticed that I did not escape my '.' in my RE. I wonder what I was thinking.

        ----------
        - Jim

        ok i tried this version because i didn't want to send people back to where they came from, but instead a more *constructive* site.

        unfortunately i'm still getting "an error occurred while processing this directive". yuck.

        any ideas?

Re: blocking naughty referrers
by Hero Zzyzzx (Curate) on May 14, 2001 at 22:56 UTC

    Hopefully you're not using the "naughty referers" idea for anything more than VERY rudimentary security. As Merlin points out it's spoofable and will flame out with all proxied users.

    Actually, re-reading your post it almost sounds like something your doing as a prank or a "revenge" thing. Is that true?

      yah i know it's not fool-proof so i'm prepared for people to slip by. i'm not doing anything sneaky-prank-y (although i'd be the first to spill the details if i were), i'm just trying to weed out some dumb people. it sounds awful trivial but to TRY and simplify... there's a person who is linking to my page and not on the most polite of terms. i'm tired of the negative comments i'm receiving because of this person and rather than continue the drama i'd like to use this approach. eh.