Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Getting username from .htaccess protection?

Getting username from .htaccess protection?

Getting username from .htaccess protection?

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,


RE: Getting username from .htaccess protection?

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


As always, I hope that helped!

Beware: Studies have shown that research causes cancer in lab rats.

RE: Getting username from .htaccess protection?

Someone else just told me to try


so I'm sure one of them will work!

Thanx for your help.


RE: Getting username from .htaccess protection?

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:

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

RE: Getting username from .htaccess protection?

Wow, that's quite a bit of work!

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



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close