Contributed by Anonymous Monk on May 10, 2000 at 09:01 UTC
Q&A  > CGI programming


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

Current script:

@referers = '',''); # 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 = ('', ''); my @urls_to_check = ('', ''); 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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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.