- Ecrit par Damien Seguy
Ce document est aussi disponible en français
HTTP headers contains a wealth of information for both the server and the brower. They are also interesting for the PHP developper. There are two kinds of headers : the one linked to the current request, which are divided into server and browser's; and there are those who are linked to PHP requesting a remote file.
From PHP point of view, one will read the browser's header in the $_SERVER superglobal, usually with index prefixed by HTTP (such as the famous $_SERVER[HTTP_AGENT],). It is also possible to reach all the raw header with the function getallheaders(), avaiable since PHP 4.
When sending headers from the Web server, one will use the header() function : who never heard about it? There are also the lesser known functions headers_sent(), which tell if headers were already sent (as no cookie can be set now), and the header_list(), which has the list of headers about to be sent.
Now, sometimes, we need to get HTTP information about another ressource. This is where get_headers() steps in. This function query a distant server, using a regular HTTP query (POST, GET, HEAD, etc) et but only read the HTTP headers before breaking contact. If the distant URL is a huge video, than means a short head download, iinstead of a huge waste of bandwidth.
<?php print_r(get_headers('http://www.nexen.net/')); ?>
This script will display :
Array (  => HTTP/1.1 200 OK  => Date: Mon, 26 Mar 2007 17:00:35 GMT  => Server: Apache/1.3.33 (Debian GNU/Linux) PHP/4.4.6-0.dotdeb.2 with Suhosin-Patch  => X-Powered-By: PHP/4.4.6-0.dotdeb.2  => Set-Cookie: 5c637dbab625dce1c47987d4173f3151=a17b2656a834f89286dc5372312646cf; expires=Tue, 27 Mar 2007 05:00:36 GMT; path=/  => Expires: Mon, 26 Jul 1997 05:00:00 GMT  => Last-Modified: Mon, 26 Mar 2007 17:00:40 GMT  => Cache-Control: no-store, no-cache, must-revalidate  => Cache-Control: post-check=0, pre-check=0  => Pragma: no-cache  => Set-Cookie: current_first_level_menu=1  => Connection: close  => Content-Type: text/html; charset=iso-8859-1 )
This first version of get_headers() returns an array, with a header by entry. The second version, using true as optionnal second arguemnt, will analyze the headers and sort them in a hash. The HTTP status will be held in index 0.
<?php print_r(get_headers('http://www.nexen.net/', true)); ?> Array (  => HTTP/1.1 200 OK [Date] => Mon, 26 Mar 2007 17:01:44 GMT [Server] => Apache/1.3.33 (Debian GNU/Linux) PHP/4.4.6-0.dotdeb.2 with Suhosin-Patch [X-Powered-By] => PHP/4.4.6-0.dotdeb.2 [Set-Cookie] => Array (  => 5c637dbab625dce1c47987d4173f3151=e8e45bb6efa28553780162dd22c75871; expires=Tue, 27 Mar 2007 05:01:44 GMT; path=/  => current_first_level_menu=1 ) [Expires] => Mon, 26 Jul 1997 05:00:00 GMT [Last-Modified] => Mon, 26 Mar 2007 17:01:48 GMT [Cache-Control] => Array (  => no-store, no-cache, must-revalidate  => post-check=0, pre-check=0 ) [X-PHP-Load] => 2.13525390625, 1.966796875, 1.5078125 [Content-Length] => 3515 [Pragma] => no-cache [Connection] => close [Content-Type] => text/html; charset=iso-8859-1 )
In this array you'll find, in no specific order, nor any warranty of presence
- HTTP response code
- Size of the file
- Caching information
- And all those headers that may be add by PHP, the script, the Web server or any other module and software invovled
You may use this information to make sure a ressource exists before using it. For example, a head on a Zip archive will tell you if the file exists, and how large is it.
Also, be warned that every header may be set by a script : basically, they may be altered in any way, and they are never checked in any way. Use this as information, but keep in mind that you are responsible for your safety.
Keep in mind
get_headers() is available since PHP 5
get_headers() uses the default context, instead of a third optional argument. Use stream_context_get_default() to set your HTTP request at will.
When using stream, you may read those information with stream_get_meta_data()
When in PHP 4, you must emulate this behavior withfsockopen()
Vous pouvez ajouter votre commentaire!
Vous devez vous connecter pour commenter