2015-12-05 144 views
1

我正在爲我的移動應用程序構建後端系統。我想用基本的HTTP驗證來保護內容。基本HTTP身份驗證在Google App Engine中不起作用

我寫了下面的代碼,它在本地主機上運行得如同微風。

if ($_SERVER['PHP_AUTH_USER'] != '[email protected]' && $_SERVER['PHP_AUTH_PW'] != '11test11') { 
    header('WWW-Authenticate: Basic realm="Denda Backend"'); 
    header('HTTP/1.0 401 Unauthorized'); 
    echo "Wrong credentials :-("; 
    exit; 
} 

但是,當我上傳應用程序到GAE時,這不起作用。我不斷收到相同的對話框。在網上也找不到任何可靠的幫助。

感謝, Krish

回答

0

我知道這是一個老問題,但它很高興知道它

if (isset($_SERVER['HTTP_AUTHORIZATION'])) { 
    $nobasic = substr($_SERVER['HTTP_AUTHORIZATION'],6);   // removing 'Basic ' from the string 
    $decoded = base64_decode($nobasic);         // decoding string with user:password 
    list($client_user, $client_pass) = explode(':',$decoded); 
    if ($client_user == "user" && $client_pass == "password") { 
     // Successfully authenticated 
    } else { 
     // Authentication failed, username or password are wrong 
     header('WWW-Authenticate: Basic realm="site"'); 
     header('HTTP/1.0 401 Unauthorized'); 
     echo "Wrong credentials :-("; 
     exit; 
    } 
} else { 
    // Not authenticated as there is no appropriate header in HTTP request 
    header('WWW-Authenticate: Basic realm="site"'); 
    header('HTTP/1.0 401 Unauthorized'); 
    echo "Wrong credentials :-("; 
    exit; 
}