Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

( #10904=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on May 10, 2000 at 09:01 UTC
Q&A  > CGI programming


Description:

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') } }

Answer: how to use require function with @referer URLs
contributed by btrott

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"; }
Answer: how to use require function with @referer URLs
contributed by httptech

See my answer here.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (7)
    As of 2015-07-03 00:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (47 votes), past polls