Sitemap
TechZenith

TechZenith: Keeping you informed and inspired by the ever-evolving world of technology.

Fix for Undefined Index: Authorization Error

2 min readFeb 14, 2025

--

The “Undefined index: Authorization” error in PHP usually means you’re trying to access the $_SERVER['Authorization'] superglobal variable, but it's not set. This variable is used to retrieve the Authorization header sent by the client, often in authentication scenarios (like Bearer tokens, Basic Auth, etc.).

Press enter or click to view image in full size
Fix for Undefined Index: Authorization Error
Fix for Undefined Index: Authorization Error

Check if the Header is Sent:

  • Problem: The client might not be sending the Authorization header at all.
  • Solution: Use isset() to check if the header exists before trying to access it:

LET SUPPOSE :Notice: Undefined index: Authorization in on line 27 in PHP

This means the Authorization header is missing from the request.

Fix

Modify your script to check if the Authorization header is set before accessing it:

Get www.jyotishgher.in’s stories in your inbox

Join Medium for free to get updates from this writer.

// Get headers and validate Authorization token

$headers = apache_request_headers();

if (!isset($headers[‘Authorization’])) {

die(json_encode([“status” => 401, “message” => “Token not provided”]));

$jwt = str_replace(“Bearer “, “”, $headers[‘Authorization’]);

Full Code-

include_once($_SERVER[‘DOCUMENT_ROOT’] . “/cmms/utils/header.php”);

require __DIR__ . ‘/../vendor/autoload.php’

// Enable error reporting for debugging

ini_set(‘display_errors’, 1);

ini_set(‘display_startup_errors’, 1);

die(json_encode([“status” => 500, “message” => “Database connection is not defined.”]));

// Get headers and validate Authorization token

$headers = apache_request_headers();

if (!isset($headers[‘Authorization’])) {

die(json_encode([“status” => 401, “message” => “Token not provided”]));

$jwt = str_replace(“Bearer “, “”, $headers[‘Authorization’]);

$FOR_DATE = $_REQUEST[‘FOR_DATE’] ?? null

$EMPNO = $_REQUEST[‘EMPNO’] ?? null

$decoded = JWT::decode($jwt, new \Firebase\JWT\Key($secret_key, ‘HS256’));

// Execute database quer

$sql = oci_parse($conn, “select ^

throw new Exception(“Failed to prepare the SQL statement.”);

$executionResult = oci_execute($sql);

throw new Exception(“Database execution error: “ . $error[‘message’]);

while ($r = oci_fetch_assoc($sql)) {

$rows[] = array_map(‘htmlentities’, $r);

“message” => “Access granted”,

} catch (\Firebase\JWT\ExpiredException $e) {

echo json_encode([“status” => 401, “message” => “Token expired”, “error” => $e->getMessage()]);

} catch (\Firebase\JWT\SignatureInvalidException $e) {

echo json_encode([“status” => 401, “message” => “Invalid signature”, “error” => $e->getMessage()]);

echo json_encode([“status” => 500, “message” => “Internal Server Error”, “error” => $e->getMessage()]);

Possible Reasons for Missing Authorization Header

Final Thoughts

✅ Fix ensures the script does not throw undefined index error.
✅ Works across different servers that might modify header case.
✅ Ensures Authorization is always validated before use. 🚀

Originally published at http://whatandroidwhy.blogspot.com.

--

--

Written by www.jyotishgher.in

Jyotishgher is a premier "Astrology as a Service" startup that redefines how astrology is practiced, learned, and applied. Jyotishgher is your trusted platform.

Published in TechZenith

TechZenith: Keeping you informed and inspired by the ever-evolving world of technology.