Getting basic-auth username in php

前端 未结 3 650
情歌与酒
情歌与酒 2021-01-17 12:27

How can I access the apache basic-auth username in a PHP script?

相关标签:
3条回答
  • 2021-01-17 12:33

    Simply access this variable:

    $_SERVER['PHP_AUTH_USER']
    
    0 讨论(0)
  • 2021-01-17 12:36

    HTTP Auth in the PHP Manual would be a good starting place.

    0 讨论(0)
  • 2021-01-17 13:00

    Check what you have in $_SERVER (print_r($_SERVER)), sometimes $_SERVER['PHP_AUTH_USER'] is not available.

    If you have value for $_SERVER["HTTP_AUTHORIZATION"], you may use:

    if (isset($_SERVER["HTTP_AUTHORIZATION"]) {
            $auth = $_SERVER["HTTP_AUTHORIZATION"];
            $auth_array = explode(" ", $auth);
            $un_pw = explode(":", base64_decode($auth_array[1]));
            $un = $un_pw[0];
            $pw = $un_pw[1];
        }
    

    (copied from https://www.codepunker.com/blog/php-a-primer-on-the-basic-authorization-header)

    0 讨论(0)
提交回复
热议问题