Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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 ( [id://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.

Replies are listed 'Best First'.
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"; }
Re: how to use require function with @referer URLs
by httptech (Chaplain) on May 10, 2000 at 16:12 UTC
    See my answer here.

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?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-28 10:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found