• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

Get username in PHP from Apache Basic Authorization

Is there a way to access the username of a Basic authorized client in PHP?
0
metalaureate
Asked:
metalaureate
2 Solutions
 
HyperBPPCommented:
http://php.net/manual/en/features.http-auth.php
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Text to send if user hits Cancel button';
    exit;
} else {
    echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
    echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}
?> 

Open in new window

0
 
HackneyCabCommented:
Just check that you're not running PHP as a CGI. The HTTP authentication feature only works when PHP is running as a module in Apache:

http://php.net/manual/en/features.http-auth.php
0
 
karoldvlCommented:
And on some installations check also:
$_SERVER["REDIRECT_REMOTE_USER"]
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now