Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting username from .htaccess protection? 1

Status
Not open for further replies.

Tupps

Programmer
Feb 25, 2001
9
GB
I have a folder protected with .htaccess and I need to get the username. Any ideas?

With php I just use $PHP_AUTH_USER, but I guess having PHP in, that won't work.

I can't find it anywhere in the docs, nor in the camel book.

Thanks in advance,

Danny
 
What about $ENV{user} or $ENV{username}

MWB As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Someone else just told me to try

$ENV{'REMOTE_USER'}

so I'm sure one of them will work!

Thanx for your help.

Danny
 
Using $ENV{'USER'} or $ENV{'REMOTE_USER'} may not work. I needed to find out which of several people was executing a cgi script called from a .htaccess protected web page and neither one was set. What I had to do was decode the basic authentication value. Here's a subroutine to do it:
Code:
sub GetLogon {

my($authtype,$authstring) = split(' ', $ENV{'HTTP_AUTHORIZATION'});

return "unknown" unless $authtype =~ /basic/i;

# You COULD use the following two lines, but it would
# include other code that you don't really need.

#use MIME::Base64 ();
#$decoded = MIME::Base64::decode($encoded);

# The following code comes from perlfaq9

# remove non-base64 chars
$authstring =~ tr[A-Za-z0-9+/][]cd;
# convert to uuencoded format
$authstring =~ tr[A-Za-z0-9+/][ -_];
# compute length byte
$len = pack("c", 32 + 0.75*length($authstring));
# uudecode
my $authstring = unpack("u", $len . $authstring);

# Now we have userid:password. Split off just the userid
my ($user,undef) = split(':', $authstring);

return $user;

} # GetLogon
 
Wow, that's quite a bit of work!

I'll copy it, and test both that and the $ENV ones

Thanks!

Danny
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top