dongzhenju3015 2014-11-11 10:21
浏览 24

PHP标头验证不接受用户和密码

I am using the following PHP Authentication script for a site, but when I enter the user and password, the script won't accept them as correct (TEST/TEST1) and it always reloads the user/pass prompt (login function).

Here is my script.

function login()
{
    header('WWW-Authenticate: Basic realm="Acceso restringido."');
    header('HTTP/1.0 401 Unauthorized');
    echo "Acceso restringido.
";
    exit;
}
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    login();
}
list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':' , base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));

// open a user/pass prompt
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    login();
} else {
    if ($_SERVER['PHP_AUTH_USER'] == 'TEST' && $_SERVER['PHP_AUTH_PW'] == 'TEST1') {
   } else {
        login();
    }
}
  • 写回答

1条回答 默认 最新

  • dpvp56187 2014-11-11 10:32
    关注

    You can try this, it works fine for me:

    session_start();
    function login() {
        header("WWW-Authenticate: Basic"); 
        header("HTTP/1.0 401 Unauthorized");
        die; 
    }
    if (!isset($_SESSION["AUTH_SUCCESS"])) {
        $_SESSION["AUTH_SUCCESS"] = 0;
    }
    if ($_SESSION["AUTH_SUCCESS"] == 0) {
        $user = "TEST"; 
        $pass = "TESTPASS";
        if ($_SERVER["PHP_AUTH_USER"] != $user || $_SERVER["PHP_AUTH_PW"] != $pass) { 
            login();
        } else {
            $_SESSION["AUTH_SUCCESS"] = 1;
        }
    }
    if ($_SESSION["AUTH_SUCCESS"] == 0) {
        die("You have entered a wrong password/username.");
    }
    

    This code holds a variable in session named AUTH_SUCCESS, as long this variable is 0 (Zero) then the prompt will appear, when the user enters the user/pass, then the variable value will become 1 and the prompt will stop appearing.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度