<?xml version="1.0" encoding="windows-1252"?>
<node id="246148" title="Re: htaccess through perl without apache" created="2003-03-26 21:39:46" updated="2005-06-08 16:43:08">
<type id="11">
note</type>
<author id="28290">
sutch</author>
<data>
<field name="doctext">
Another option is to not deal with the htpasswd file and store and test user credentials within your program.  For example:

&lt;code&gt;
use CGI;
my $request = CGI-&gt;new;
if( $ENV{'REMOTE_USER'} eq "sutch" &amp;&amp; $ENV{'REMOTE_PASSWD' } eq "myb4d" ) {
  # user is authenticated
  print $request-&gt;header;
  # return restricted web page here
} else {
  print $request-&gt;header( '-status' =&gt; '401 Authentication required', '-auth-type' =&gt; 'Basic', '-WWW-Authenticate' =&gt; 'Basic realm="My Restricted Area"' );
  # return failed authentication message here
}
&lt;/code&gt;

This will provide the user with the familiar username/password dialog box that is displayed when using htaccess.  Instead of Apache handling the authentication though, the script tests the REMOTE_USER and REMOTE_PASSWD environment variables to authenticate the user.&lt;p&gt;

A benefit of handling the authentication yourself is that you can also expire authenticated sessions and allow users to logout.  This can be done by returning a 401 status with different realm text.</field>
<field name="root_node">
246048</field>
<field name="parent_node">
246048</field>
</data>
</node>
