Search code examples
phpsymfonysymfony-http-foundation

How do I retrieve basic auth credentials from Symfony's HttpFoundation component?


I want to retrieve the basic authentication credentials from Symfony's HttpFoundation component. A cursory look at the docs reveals nothing; is there a way I can do it?

(I'm using the HttpFoundation component in an application not based on the Symfony framework.)


Solution

  • The values of $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] can be accessed through an instance of Symfony\Component\HttpFoundation\Request in the following manner:

    $username = $request->headers->get('php-auth-user');
    $password = $request->headers->get('php-auth-pw');