EKSYAM.COM – PHP Header Authorization Tidak Berfungsi. Header Authorization berisi kredensial untuk mengautentikasi user agent dengan server (proxy). Di sini, type Authorization diperlukan dan diikuti oleh kredensial, yang dapat dikodekan atau dienkripsi tergantung pada skema otentikasi (Basic, Bearer, Digest, HOBA, Mutual, AWS4-HMAC-SHA256, Dll) yang digunakan.

Struktur Authorization:
Authorization: <type> <credentials>
Contoh Authorization :
Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l
Authorization: Bearer eyJ0eXAiOiJqd3QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEiLCJtZW1iZXJfaWQiOiIxIiwibWVtYmVyX2FjY291bnRfaWQiOiIxIiwiZ3JhbnRfdHlwZSI6ImNsaWVudF9jcmVkZW50aWFscyIsImNsaWVudF9pZCI6ImNsaWVudC1jcmUtaDJoLUgySDE4MDMwMSIsImNsaWVudF9zZWNyZXQiOiIhOSQwMDk5ODc4NzUiLCJzY29wZSI6InNjb3BlLXR4cGF5LWgyaGFwaSIsImlwIjoiMTE5LjIuNTIuMjM5In0.bxH5932_ODePvdO327G2asuYhPoBXg5W1EuVAqZnclQ
Cara memangil Authorization di server seperti berikut:
$requestHeaders = apache_request_headers();
$requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));
$authorization = trim($requestHeaders['Authorization']);Dibeberapa server terdapat kasus bahwa fungsi apache_request_headers() tidak dapat berjalan sehingga PHP Header Authorization tidak berfungsi, muncul pesan sebagai berikut:
Fatal error: Call to undefined function apache_request_headers()
Fungsi dengan awalan apache_ memang hanya tersedia jika PHP berjalan dalam modus modul (mod_php4/mod_php5). Di server shared hosting Linux kebanyakan PHP berjalan sebagai CGI/FastCGI, untuk faktor keamanan/pemisahan antar pemakai.
Sebagai solusinya jika PHP Header Authorization tidak berfungsi Anda dapat menggunakan script berikut:
<?php
$arh = array();
$rx_http = '/\AHTTP_/';
foreach($_SERVER as $key => $val) {
if( preg_match($rx_http, $key) ) {
$arh_key = preg_replace($rx_http, '', $key);
$rx_matches = array();
// do some nasty string manipulations to restore the original letter case
// this should work in most cases
$rx_matches = explode('_', $arh_key);
if( count($rx_matches) > 0 and strlen($arh_key) > 2 ) {
foreach($rx_matches as $ak_key => $ak_val) $rx_matches[$ak_key] = ucfirst($ak_val);
$arh_key = implode('-', $rx_matches);
}
$arh[$arh_key] = $val;
}
}
$authorization = $arh['AUTHORIZATION'];Tambahkan script dibawah ini di .htaccess
RewriteEngine On
RewriteRule .* - [e=HTTP_AUTHORIZATION:%{HTTP:Authorization}]Jika sudah silahkan jalankan script diatas dengan mengirimkan kredensial di Header Authorization dari sisi client, saya mencobanya menggunakan postman (cara instal aplikasi postman). Berikut berhasil menampilkan kredensial Authorization-nya, tinggal Anda mengelola sesuai dengan kebutuhan.

Sampai tahap ini jika Anda juga berhasil berarti masalah PHP Header Authorization tidak berfungsi sudah beres. Berikut sebagai tambahan saya buatkan function getAuthorizationHeader() untuk menampilkan kredensial Authorization:
<?php
function getAuthorizationHeader(){
if (isset($_SERVER['Authorization']))
return trim($_SERVER["Authorization"]);
if (isset($_SERVER['HTTP_AUTHORIZATION']))
return trim($_SERVER["HTTP_AUTHORIZATION"]);
if (function_exists('apache_request_headers')) {
$requestHeaders = apache_request_headers();
$requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));
if (isset($requestHeaders['Authorization']))
return trim($requestHeaders['Authorization']);
} else {
$arh = array();
$rx_http = '/\AHTTP_/';
foreach($_SERVER as $key => $val) {
if( preg_match($rx_http, $key) ) {
$arh_key = preg_replace($rx_http, '', $key);
$rx_matches = array();
$rx_matches = explode('_', $arh_key);
if( count($rx_matches) > 0 and strlen($arh_key) > 2 ) {
foreach($rx_matches as $ak_key => $ak_val) $rx_matches[$ak_key] = ucfirst($ak_val);
$arh_key = implode('-', $rx_matches);
}
$arh[$arh_key] = $val;
}
}
return $arh['AUTHORIZATION'];
}
return null;
}Demikianlah tutorial cara mengatasi PHP Header Authorization Tidak Berfungsi, semoga bermanfaat.

KOMENTAR TERBARU