1. 程式人生 > >PHP中的HTTP認證

PHP中的HTTP認證

php伺服器端獲取方法

if ($_SERVER['PHP_AUTH_USER'] != 'admin' && $_SERVER['PHP_AUTH_PW' != '123']) {
    header('WWW-Authenticate: Basic realm="Auth failed"');
    header('HTTP/1.0 401 Unauthorized');
}

php客戶端請求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://test.local");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'admin:123');
$data = curl_exec($ch);
curl_close($ch);