I have a really frustrating issue where I cannot retrieve any of the headers. Here is my code:
$headers = getallheaders();
echo($headers["SystemTime"]); //Doesnt work
$keys = array_keys($headers);
echo($headers[$keys[4]]); //Doesnt work
Both lines produce the error 'Undefined index: SystemTime'.
I cannot for the life of me figure out why I cant get the value. If I go print_r($headers);
I get this
Array
(
[Content-Type] => application/x-www-form-urlencoded
[Content-Length] => 0
[Host] => localhost
[Referer] =>
[SystemTime] => 2012-06-26+09%3a20%3a27
)
var_dump of $headers
array(5) {
["Content-Type"]=>
string(33) "application/x-www-form-urlencoded"
["Content-Length"]=>
string(1) "0"
["Host"]=>
string(9) "localhost"
["Referer"]=>
string(0) ""
["SystemTime"]=>
string(23) "2012-06-26+10%3a10%3a08"
}
var_dump of $keys
array(5) {
[0]=>
string(12) "Content-Type"
[1]=>
string(14) "Content-Length"
[2]=>
string(4) "Host"
[3]=>
string(7) "Referer"
[4]=>
string(10) "SystemTime"
}
foreach ($headers as $name => $value) {
echo "$name: $value. From Array: {$headers[$name]}\n";
}
returned:
Connection: Keep-Alive. From Array: Keep-Alive
Content-Type: application/x-www-form-urlencoded. From Array: application/x-www-form-urlencoded
Content-Length: 0. From Array: 0
Host: localhost. From Array: localhost
Referer: . From Array:
Notice: Undefined index: SystemTime in /clientdata/apache-www/a/d/[XXXXXX].com/www/admin/request/GetPCLicence.php on line 22
SystemTime: 2012-06-26+10%3a10%3a08. From Array:
Im stuck and I seriously cannot figure out what is going wrong. It should work.
PS. I know that the SystemTime header is non-standard. I provide that from my http_request.
Undefined Index in PHP is a Notice generated by the language. The simplest way to ignore such a notice is to ask PHP to stop generating such notices. You can either add a small line of code at the top of the PHP page or edit the field error_reporting in the php. ini file.
To solve such error, you can use the isset() function, which will check whether the index or variable is set or not, If not then don't use it.
Notice Undefined Index in PHP is an error which occurs when we try to access the value or variable which does not even exist in reality. Undefined Index is the usual error that comes up when we try to access the variable which does not persist.
How To Solve Warning: Undefined array key Error ? To Solve Warning: Undefined array key Error You Just need to check that what you are trying to get value is exists or Not So you have to use isset. Just like this. Now, Your error must be solved.
I got it after much help from you guys. I had a feeling that because it worked after the deserializing that maybe the encoding was different and thats why we could see it, but not touch it?
My code below gets all the headers and converts the encoding and puts it in a new array :)
Here is my 'Translation' code.
$headersRaw = getallheaders();
$headers = array();
foreach($headersRaw as $key => $value)
{
$headers[mb_convert_encoding($key, "UTF-8")] = $value;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With