Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How do I make my script behave differently based on the referring URL?

by Anonymous Monk
on May 10, 2000 at 09:01 UTC ( #10904=perlquestion: print w/replies, xml ) Need Help??

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

How do I change a script that checks refering URLs from within to a seperate file with the require function?

Current script:

@referers = 'http://www.mydomain.com','http://www.another.com'); # Check Referring URL &check_url; sub check_url { local($check_referer) = 0; if ($ENV{'HTTP_REFERER'}) { foreach $referer (@referers) { if ($ENV{'HTTP_REFERER'} =~ /$referer/i) { $check_referer = 1; last; } } } else { $check_referer = 1; } if ($check_referer != 1) { &error('bad_referer') } }

Originally posted as a Categorized Question.

  • Comment on How do I make my script behave differently based on the referring URL?
  • Download Code

Replies are listed 'Best First'.
Re: how to use require function with @referer URLs
by httptech (Chaplain) on May 10, 2000 at 16:12 UTC
    See my answer here.
Re: how to use require function with @referer URLs
by btrott (Parson) on May 10, 2000 at 09:12 UTC
    You'll want to modify your check_url function so that it accepts a URL or a list of URLs as an argument; then you're not depending on a global variable.

    You may also want to take the error-handling and error-invocation out of the subroutine (since it sounds like you're moving the subroutine into a library of some sort) and let your application code deal with the error.

    Personally, I'd change the subroutine to accept a list of valid referrers and a URL to check, then return either 1 for a good url or 0 (or undef) for a bad URL.

    Something like this:

    sub check_url { local $_ = shift; my $valid_referrers = shift; my $valid = join '|', @$valid_referrers; return /$valid/ ? 1 : 0; }
    You can use this something like this:
    my @referrers = ('http://www.foo.com/', 'http://www.bar.com/'); my @urls_to_check = ('http://www.baz.com/bar/', 'http://www.foo.com/bar/'); for my $url (@urls_to_check) { print $url, ": ", check_url($url, \@referrers), "\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://10904]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2021-09-28 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?