0

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

still not working in my environment while it is working in the fiddle site, Do you have a clue?
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.