The quick-n-dirty answer would be to read up on apache .htaccess files.

The better answer is to read up on Cookies and the PerlAccessHandler directive in Apache. When Apache processes a URL request, that request goes through several phases, one of which is ("Is this user allowed to view this URL?"). By writing your own PerlAccessHandler, you can protect your site against users who aren't logged in.

A quick example (I'll try to minimize the errors :)

package MyAccessHandler; use strict; use warnings; use Apache::Request; use Apache::Cookie; use Apache; sub handler { my $r = shift; my $url = $r->hostname . $r->uri(); my $file_path = $r->parsed_uri()->unparse(); # do not protect login page. return Apache::OK if $file_path=~ /login\.html$/; my $can_access = 0; ## If the user has a cookie, see if user has access. my $cookie = eval{ ({Apache::Cookie->new($r)->parse()}->{cookie}->value) }; if ($cookie) { $can_access = MyValidationRoutine($cookie); } # return OK if they're allowed return Apache::OK if ($can_access); # otherwise, redirect $r->internal_redirect('/login.html'); return Apache::Constants::DONE(); }

Things to read up on: Apache::Request, Apache::Cookie. O'Reilly's "Apache Modules with C and Perl" is a good resource as well, though it's a little out of date and written for Apache 1.3.x, not 2.0.x. Is there a good Apache 2.0.x book out there?

Side note - this was written for Apache 1.3.x - I know things have changed in 2.0.x, but the same general idea applies.


In reply to Re: CGI security by swngnmonk
in thread CGI security by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.