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 "<P>\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 "<P>\n";
echo "Cookie = $Cookie<BR>\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
headers_list
(PHP 5)
headers_list — 送信した (もしくは送信される予定の) レスポンスヘッダの一覧を返す
説明
array headers_list ( void )headers_list() はブラウザもしくはクライアントに送信されるヘッダの数値配列を返します。 これらのヘッダが送信されたかどうかを判断するためには headers_sent() を使用します。
例 1553. headers_list() の使用例
<?php
/* setcookie() でレスポンスヘッダをそれ自身に追加します */
setcookie('foo', 'bar');
/* 独自のレスポンスヘッダを定義します。
これはほとんどのクライアントで無視される */
header("X-Sample-Test: foo");
/* レスポンスがプレーンテキストだと宣言します */
header('Content-type: text/plain');
/* 送信しようとしているヘッダは? */
var_dump(headers_list());
?>
この出力は次の通りです。
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"
}
headers_sent()、 header() および setcookie() も参照ください。
headers_list
leo at leoberkhout dot nl
24-Oct-2006 05:40
24-Oct-2006 05:40
robertbienert at gmx dot net
27-Sep-2006 10:59
27-Sep-2006 10:59
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.
Luke at Bonanomi dot com
02-Sep-2006 10:56
02-Sep-2006 10:56
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
)