Ahmad Abdelwahed
Ahmad Abdelwahed

Reputation: 1

PHP Headers Parsing

I am having a problem parsing http headers with PHP.

When I run this code:

$headers = getallheaders();

all headers are loaded into the $headers array. However, if any array key name has a "-" in it, then the corresponding result is null. For example:

echo $headers[User-Agent];

will return null, whereas:

echo $headers[Host];

will return the value normally.

Is this a bug or I am missing something?

Upvotes: 0

Views: 88

Answers (2)

vfsoraki
vfsoraki

Reputation: 2307

Try dumping $headers and see what is in it.

var_dump($headers);

Find available keys, and use them.

var_dump($headers['User-Agent']);

Note that keys are case sensitive.

Upvotes: 1

Abhijith Sasikumar
Abhijith Sasikumar

Reputation: 14980

Just try using doube/single quotes like the below code:

<?php
  $headers = getallheaders();
  echo $headers["User-Agent"];
  echo $headers["Host"];
?>

Output:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/44.0.2403.89 Chrome/44.0.2403.89 Safari/537.36main.xfiddle.com

Try this code in php fiddle

Upvotes: 1

Related Questions