Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Network Functions : headers_list

headers_list

Returns a list of response headers sent (or ready to send) (PHP 5)
array headers_list ( )

headers_list() will return a list of headers to be sent to the browser / client. To determine whether or not these headers have been sent yet, use headers_sent().

Return Values

Returns a numerically indexed array of headers.

Examples

Example 1602. Examples using headers_list()

<?php

/* setcookie() will add a response header on its own */
setcookie('foo', 'bar');

/* Define a custom response header
  This will be ignored by most clients */
header("X-Sample-Test: foo");

/* Specify plain text content in our response */
header('Content-type: text/plain');

/* What headers are going to be sent? */
var_dump(headers_list());

?>

The above example will output:

array(4) {
 [0]=>
 string(23) "X-Powered-By: PHP/5.1.3"
 [1]=>
 string(19) "Set-Cookie: foo=bar"
 [2]=>
 string(18) "X-Sample-Test: foo"
 [3]=>
 string(24) "Content-type: text/plain"
}


Code Examples / Notes » headers_list

leo

If I change in de above code "php.net" for antoher URL, de code doesn't work properly.
How can it be made in a way like this:
http://webtools.live2support.com/header.php
I spent a hole evening, but I found only a way to make the output more readable:
<?php
function get_headers($host, $url)
{
$headers = array();
$fp = fsockopen ($host, 80, $errno, $errstr, 45);
if ($fp)
{
fputs ($fp, "GET $url HTTP/1.0\r\n\r\n");
while (!feof($fp))
{
$char = fgetc($fp);
if($char === "\n")
{
if (ord($header) === 13) { return($headers); }
else { array_push($headers, trim($header)); }
unset($header);
}
else { $header = $header.$char; }
}
fclose ($fp);
}
}
$Array = get_headers("php.net", "/");
// print_r(get_headers("php.net", "/"));
echo "

\n";
foreach ($Array as $Key => $Value)
{
echo "<LI>\$_SERVER[\"$Key\"]=$Value\n";
if ($Key == 6)
 $Cookie = $Value;
} # End of foreach ($_SERVER as $Key=>$Value)
 echo "

\n";
 echo "Cookie = $Cookie
\n";
?>
Here I do a check with, for example, key 6, to seperated them from the other values, just for example.
My problem is that the code does not accept www.mysite.com or http://www.mysite.com


ejsexton82

I don't know if there is already a function for this or not, but I wrote one anyways.
<?PHP
function headers_array() {
$return = array();
$headers = headers_list();
foreach($headers as $header) {
if(strpos($header,':') === false) $return[0] = $header;
else {
$parsed = explode(':',$header,2);
$return[$parsed[0]] = trim($parsed[1]);
}
}
return $return;
}

var_dump(headers_array());
?>
yeilds:
array(5) {
 ["X-Powered-By"]=>
 string(9) "PHP/5.2.4"
 ["Expires"]=>
 string(29) "Thu, 19 Nov 1981 08:52:00 GMT"
 ["Cache-Control"]=>
 string(62) "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
 ["Pragma"]=>
 string(8) "no-cache"
 ["Content-type"]=>
 string(9) "text/html"
}


luke

I arrived here looking for a way to collect headers sent by a remote server. No such luck; so:
<?php
function get_headers($host, $url)
{
  $headers = array();
  $fp = fsockopen ($host, 80, $errno, $errstr, 45);
  if ($fp)
  {
     fputs ($fp, "GET $url HTTP/1.0\r\n\r\n");
     while (!feof($fp))
     {
        $char = fgetc($fp);
        if($char === "\n")
        {
           if (ord($header) === 13) {  return($headers); }
           else { array_push($headers, trim($header)); }
           unset($header);
        }
        else { $header = $header.$char; }
     }
     fclose ($fp);
  }
}
?>
print_r(get_headers("php.net", "/"));
Array
(
   [0] => HTTP/1.1 200 OK
   [1] => Date: Sat, 02 Sep 2006 13:47:02 GMT
   [2] => Server: Apache/1.3.37 (Unix) PHP/5.2.0-dev
   [3] => X-Powered-By: PHP/5.2.0-dev
   [4] => Last-Modified: Sat, 02 Sep 2006 13:21:09 GMT
   [5] => Content-language: en
   [6] => Set-Cookie: COUNTRY=USA%2C68.37.136.230; expires=Sat, 09-Sep-2006 13:47:02 GMT; path=/; domain=.php.net
   [7] => Connection: close
   [8] => Content-Type: text/html; charset=utf-8
)


robertbienert

Also note that Lukes code could be simplified if using the HTTP HEAD method, because it returns only the HTTP headers, not the whole resource.

Change Language


Follow Navioo On Twitter
checkdnsrr
closelog
debugger_off
debugger_on
define_syslog_variables
dns_check_record
dns_get_mx
dns_get_record
fsockopen
gethostbyaddr
gethostbyname
gethostbynamel
getmxrr
getprotobyname
getprotobynumber
getservbyname
getservbyport
header
headers_list
headers_sent
inet_ntop
inet_pton
ip2long
long2ip
openlog
pfsockopen
setcookie
setrawcookie
socket_get_status
socket_set_blocking
socket_set_timeout
syslog
eXTReMe Tracker