Szczepan Hołyszewski
Szczepan Hołyszewski

Reputation: 3175

Access HTTP authentication username in PHP?

Is it possible in PHP to access the login name of a user authenticated with server's native HTTP digest authentication (as starkly opposed to a PHP implementation of the same HTTP authentication protocol)?

Upvotes: 1

Views: 425

Answers (1)

Eborbob
Eborbob

Reputation: 1975

The data you are after is provided in the HTTP header sent from the client (as per https://www.rfc-editor.org/rfc/rfc2617 ), namely the header Authorization.

In PHP this header can be retrieved like so:

$header = $_SERVER['HTTP_AUTHORIZATION'];

The header contains a number of key value pairs separated by commas. The username is in the username key, you can extract this by something like:

preg_match('/username="([^"]+)"/', $header, $matches);
$username = $matches[1];

Upvotes: 1

Related Questions