Source for file HTTP.php
Documentation is available at HTTP.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Stig Bakken <ssb@fast.no> |
// | Sterling Hughes <sterling@php.net> |
// | Tomas V.V.Cox <cox@idecnet.com> |
// | Richard Heyes <richard@php.net> |
// | Philippe Jausions <Philippe.Jausions@11abacus.com> |
// | Michael Wallner <mike@php.net> |
// +----------------------------------------------------------------------+
// $Id: HTTP.php,v 1.26 2004/06/10 21:54:53 mike Exp $
* @version $Revision: 1.26 $
* Format a RFC compliant GMT date HTTP header. This function honors the
* "y2k_compliance" php.ini directive and formats the GMT date corresponding
* to either RFC850 or RFC822.
* @return mixed GMT date string, or false for an invalid $time parameter
* @param mixed $time unix timestamp or date (default = current time)
function Date($time = null )
$format = ini_get('y2k_compliance') ? 'D, d M Y' : 'F, d-D-y';
return gmdate($format . ' H:i:s \G\M\T', $time);
* Negotiate language with the user's browser through the Accept-Language
* HTTP header or the user's host address. Language codes are generally in
* the form "ll" for a language spoken in only one country, or "ll-CC" for a
* language spoken in a particular country. For example, U.S. English is
* "en-US", while British English is "en-UK". Portugese as spoken in
* Portugal is "pt-PT", while Brazilian Portugese is "pt-BR".
* Quality factors in the Accept-Language: header are supported, e.g.:
* Accept-Language: en-UK;q=0.7, en-US;q=0.6, no, dk;q=0.8
* require_once 'HTTP.php';
* 'en-US'=> 'locales/en',
* 'en-UK'=> 'locales/en',
* 'de-DE'=> 'locales/de',
* 'de-AT'=> 'locales/de',
* $neg = HTTP::negotiateLanguage($langs);
* @return string The negotiated language result or the supplied default.
* @param array $supported An associative array of supported languages,
* whose values must evaluate to true.
* @param string $default The default language to use if none is found.
foreach ($supported as $lang => $isSupported) {
$supp[strToLower ($lang)] = $lang;
if (isset ($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) {
$matches[$l] = 1000 - count($matches);
asort($matches, SORT_NUMERIC );
if (isset ($_SERVER['REMOTE_HOST'])) {
if (isset ($supp[$lang])) {
* Sends a "HEAD" HTTP command to a server and returns the headers
* as an associative array. Example output could be:
* [response_code] => 200 // The HTTP response code
* [response] => HTTP/1.1 200 OK // The full HTTP response string
* [Date] => Fri, 11 Jan 2002 01:41:44 GMT
* [Server] => Apache/1.3.20 (Unix) PHP/4.1.1
* [X-Powered-By] => PHP/4.1.1
* [Content-Type] => text/html
* @see HTTP_Client::head()
* @return mixed Returns associative array of response headers on success
* or PEAR error on failure.
* @param string $url A valid URL, e.g.: http://pear.php.net/credits.php
* @param integer $timeout Timeout in seconds (default = 10)
function head($url, $timeout = 10 )
if (!isset ($p['scheme'])) {
} elseif ($p['scheme'] != 'http') {
$port = isset ($p['port']) ? $p['port'] : 80;
if (!$fp = @fsockopen($p['host'], $port, $eno, $estr, $timeout)) {
$path = !empty ($p['path']) ? $p['path'] : '/';
$path .= !empty ($p['query']) ? '?' . $p['query'] : '';
fputs($fp, " HEAD $path HTTP/1.0\r\n" );
fputs($fp, 'Host: ' . $p['host'] . ':' . $port . "\r\n");
fputs($fp, "Connection: close\r\n\r\n");
if (preg_match("|^HTTP/[^\s]*\s(.*?)\s|", $response, $status)) {
$headers['response_code'] = $status[1 ];
$headers['response'] = $response;
while ($line = fgets($fp, 4096 )) {
if (($pos = strpos($line, ':')) !== false ) {
$header = substr($line, 0 , $pos);
$headers[$header] = $value;
* This function redirects the client. This is done by issuing
* a "Location" header and exiting if wanted.
* @return mixed Returns true on succes (or exits) or false if headers
* have already been sent.
* @param string $url URL where the redirect should go to.
* @param bool $exit Whether to exit immediately after redirection.
* This function returns the absolute URI for the partial URL passed.
* The current scheme (HTTP/HTTPS), host server, port, current script
* location are used if necessary to resolve any relative URLs.
* Offsets potentially created by PATH_INFO are taken care of to resolve
* relative URLs to the current script.
* You can choose a new protocol while resolving the URI. This is
* particularly useful when redirecting a web browser using relative URIs
* and to switch from HTTP to HTTPS, or vice-versa, at the same time.
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
* @return string The absolute URI.
* @param string $url Absolute or relative URI the redirect should go to.
* @param string $protocol Protocol to use when redirecting URIs.
* @param integer $port A new port number.
function absoluteURI($url, $protocol = null , $port = null )
// Mess around with already absolute URIs
if (empty ($protocol) && empty ($port)) {
if (!empty ($_SERVER['HTTP_HOST'])) {
list ($host) = explode(':', $_SERVER['HTTP_HOST']);
} elseif (!empty ($_SERVER['SERVER_NAME'])) {
list ($host) = explode(':', $_SERVER['SERVER_NAME']);
$protocol = isset ($_SERVER['HTTPS']) ? 'https' : 'http';
if (!isset ($port) || $port != intval($port)) {
$port = $_SERVER['SERVER_PORT'];
$server = $protocol . '://'. $host . (isset ($port) ? ':'. $port : '');
if (isset ($_SERVER['PATH_INFO'])) {
$path = dirname($_SERVER['PHP_SELF']);
return $server . strtr($path, '\\', '/') . $url;
* Lazy raising of PEAR_Errors.
* @return object PEAR_Error
return PEAR ::raiseError ($error, $code);
Documentation generated on Mon, 11 Mar 2019 10:17:27 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|