STATUS_100
STATUS_100 = '100 Continue'
Header2_Cache
This package provides methods to easier handle caching of HTTP pages. That means that the pages can be cached at the client (user agent or browser) and your application only needs to send "hey client you already have the pages".
Which is done by sending the HTTP-Status "304 Not Modified", so that your application load and the network traffic can be reduced, since you only need to send the complete page once. This is really an advantage e.g. for generated style sheets, or simply pages that do only change rarely.
Usage:
require_once 'HTTP/Header/Cache.php';
$httpCache = new Header2_Cache(4, 'weeks');
$httpCache->sendHeaders();
// your code goes here
setHeader(string $key, string $value = null) : boolean
Set Header
The default value for the Last-Modified header will be current date and time if $value is omitted.
string | $key | The name of the header. |
string | $value | The value of the header. (NULL to unset header) |
Returns true on success or false if $key was empty or $value was not of an scalar type.
sendHeaders(array $keys = array(), array $include = true) : boolean
Send Headers
Send out the header that you set via setHeader().
array | $keys | Headers to (not) send, see $include. |
array | $include | If true only $keys matching headers will be sent, if false only header not matching $keys will be sent. |
Returns true on success or false if headers are already sent.
sendStatusCode(integer $code) : boolean
Send Satus Code
Send out the given HTTP-Status code. Use this for example when you want to tell the client this page is cached, then you would call sendStatusCode(304). \Pluf\HTTP\Header2_Cache::exitIfCached()
integer | $code | The status code to send, i.e. 404, 304, 200, etc. |
Returns true on success or false if headers are already sent.
dateToTimestamp(string $date) : mixed
Date to Timestamp
Converts dates like Mon, 31 Mar 2003 15:26:34 GMT Tue, 15 Nov 1994 12:45:26 GMT into a timestamp, strtotime() didn't do it in older versions.
string | $date | The GMT date. |
Returns int unix timestamp or false if the date doesn't seem to be a valid GMT date.
redirect(string $url, boolean $exit = true, boolean $rfc2616 = false) : boolean
This function redirects the client.
This is done by issuing a "Location" header and exiting if wanted. If you set $rfc2616 to true HTTP will output a hypertext note with the location of the redirect.
string | $url | URL where the redirect should go to. |
boolean | $exit | Whether to exit immediately after redirection. |
boolean | $rfc2616 | Wheter to output a hypertext note where we're redirecting to (Redirecting to ....) |
Returns TRUE on succes (or exits) or FALSE if headers have already been sent.
None found |
getStatusType(string $http_code) : integer|false
Return HTTP Status Code Type
string | $http_code | HTTP status code |
None found |
getStatusText(string $http_code) : string|false
Return Status Code Message
string | $http_code | HTTP status code |
None found |
isInformational(string $http_code) : boolean
Checks if HTTP Status code is Information (1xx)
string | $http_code | HTTP status code |
None found |
isSuccessful(string $http_code) : boolean
Checks if HTTP Status code is Successful (2xx)
string | $http_code | HTTP status code |
None found |
isRedirect(string $http_code) : boolean
Checks if HTTP Status code is a Redirect (3xx)
string | $http_code | HTTP status code |
None found |
isClientError(string $http_code) : boolean
Checks if HTTP Status code is a Client Error (4xx)
string | $http_code | HTTP status code |
None found |
isServerError(string $http_code) : boolean
Checks if HTTP Status code is Server Error (5xx)
string | $http_code | HTTP status code |
None found |
isError(string $http_code) : boolean
Checks if HTTP Status code is Server OR Client Error (4xx or 5xx)
string | $http_code | HTTP status code |
None found |
date(mixed $time = null) : string|boolean
Formats 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.
mixed | $time | unix timestamp or date (default = current time) |
GMT date string, or FALSE for an invalid $time parameter
None found |
negotiateLanguage(array $supported, string $default = 'en-US') : string
Negotiates 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 'HTTP2.php';
$http = new HTTP2();
$langs = array(
'en' => 'locales/en',
'en-US' => 'locales/en',
'en-UK' => 'locales/en',
'de' => 'locales/de',
'de-DE' => 'locales/de',
'de-AT' => 'locales/de',
);
$neg = $http->negotiateLanguage($langs);
$dir = $langs[$neg];
array | $supported | An associative array of supported languages, whose values must evaluate to true. |
string | $default | The default language to use if none is found. |
The negotiated language result or the supplied default.
None found |
negotiateCharset(array $supported, string $default = 'ISO-8859-1') : string
Negotiates charset with the user's browser through the Accept-Charset HTTP header.
Quality factors in the Accept-Charset: header are supported, e.g.: Accept-Language: en-UK;q=0.7, en-US;q=0.6, no, dk;q=0.8
require_once 'HTTP2.php';
$http = new HTTP2();
$charsets = array(
'UTF-8',
'ISO-8859-1',
);
$charset = $http->negotiateCharset($charsets);
array | $supported | An array of supported charsets |
string | $default | The default charset to use if none is found. |
The negotiated language result or the supplied default.
author |
Philippe Jausions jausions@php.net |
---|
negotiateMimeType(array $supported, string $default) : string
Negotiates content type with the user's browser through the Accept HTTP header.
Quality factors in the Accept: header are supported, e.g.: Accept: application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8
require_once 'HTTP2.php';
$http = new HTTP2();
$contentType = array(
'application/xhtml+xml',
'application/xml',
'text/html',
'text/plain',
);
$mime = $http->negotiateMimeType($contentType);
array | $supported | An associative array of supported MIME types. |
string | $default | The default type to use if none match. |
The negotiated MIME type result or the supplied default.
author |
Philippe Jausions jausions@php.net |
---|
head(string $url, integer $timeout = 10) : array
Sends a "HEAD" HTTP command to a server and returns the headers as an associative array.
Example output could be:
Array
(
[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
[Connection] => close
[Content-Type] => text/html
)
string | $url | A valid URL, e.g.: http://pear.php.net/credits.php |
integer | $timeout | Timeout in seconds (default = 10) |
When connecting fails
When the protocol is not uspported
Returns associative array of response headers on success
None found |
absoluteURI(string $url = null, string $protocol = null, integer $port = null) : string
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.
string | $url | Absolute or relative URI the redirect should go to. |
string | $protocol | Protocol to use when redirecting URIs. |
integer | $port | A new port number. |
The absolute URI.
author |
Philippe Jausions Philippe.Jausions@11abacus.com |
---|
parseLinks(string|array $lines) : array
Parses a HTTP "Link" header value as specified by RFC 5988.
When errors occur during parsing, all information collected to that point will be returned used and the rest of the line skipped.
string|array | $lines | HTTP "Link:" header value (without "Link:"), or array of such strings |
Array of parsed links (one single line may contain multiple links) Only params existing in the link are in the array. URI is available in key "_uri".
None found |
getCacheStart() : integer
Get Cache Start
Returns the unix timestamp of the If-Modified-Since HTTP header or the current time if the header was not sent by the client.
unix timestamp
None found |
isOlderThan(integer $time, string $unit = 'seconds') : boolean
Is Older Than
You can call it like this:
$httpCache->isOlderThan(1, 'day');
$httpCache->isOlderThan(47, 'days');
$httpCache->isOlderThan(1, 'week'); $httpCache->isOlderThan(3, 'weeks');
$httpCache->isOlderThan(1, 'hour'); $httpCache->isOlderThan(5, 'hours');
$httpCache->isOlderThan(1, 'minute'); $httpCache->isOlderThan(15, 'minutes');
$httpCache->isOlderThan(1, 'second'); $httpCache->isOlderThan(15);
If you specify something greater than "weeks" as time untit, it just works approximatly, because a month is taken to consist of 4.3 weeks.
integer | $time | The amount of time. |
string | $unit | The unit of the time amount - (year[s], month[s], week[s], day[s], hour[s], minute[s], second[s]). |
Returns true if requested page is older than specified.
None found |
isCached(integer $lastModified) : boolean
Is Cached
Check whether we can consider to be cached on the client side.
integer | $lastModified | Unix timestamp of last modification. |
Whether the page/resource is considered to be cached.
None found |
None found |
exitIfCached(integer $lastModified) : void
Exit If Cached
Exit with "HTTP 304 Not Modified" if we consider to be cached.
integer | $lastModified | Unix timestamp of last modification. |
None found |
None found |
setLastModified(integer $lastModified = null) : void
Set Last Modified
integer | $lastModified | The unix timestamp of last modification. |
None found |
matchAccept(string $header, array $supported) : string|NULL
Parses a weighed "Accept" HTTP header and matches it against a list of supported options
string | $header | The HTTP "Accept" header to parse |
array | $supported | A list of supported values |
a matched option, or NULL if no match
None found |
sortAccept(string $header) : array
Parses and sorts a weighed "Accept" HTTP header
string | $header | The HTTP "Accept" header to parse |
Sorted list of "accept" options
None found |
convertCharset(string $from, string $to, string $str) : string
Convert the given string from one into another charset.
Uses mb_convert_encoding or iconv if available.
string | $from | Source charset the string is in |
string | $to | Target character set |
string | $str | String to convert |
converted string
None found |