Request Information

The request library provides a wrapper around request information, for example the request URI, GET and POST variables, etc.

CSF_Request

Options

Name Description Default
preserve_original Make a copy of request variables rather than modifying the superglobals1) true
fix_magic_quotes Detect if magic_quotes_gpc is enabled and undo the effects if it is true
uri_protocol Method for getting the request: AUTO, GET (see below), REQUEST_URI (not implemented) or a server variable name AUTO
uri_get_variable GET variable to use if uri_protocol = “GET” _uri

GET, POST, COOKIE, REQUEST

The four read-only request superglobals $_GET, $_POST, $_COOKIE and $_REQUEST are made available as public member variables of CSF_Request. Regardless of the value of the preserve_original option, these public variables will always point to values which have been processed by the constructor, for example having had magic_quotes_gpc slashes removed.

// Example: request for /index.php?foo=this'has"quotes
$request = new CSF_Request(array(
    'preserve_original' => true,
    'fix_magic_quotes' => true
));
echo $_GET['foo']
// Ouput: this\'has\"quotes
echo $request->GET['foo']
// Output: this'has"quotes

extract_GET, extract_POST, extract_COOKIE, extract_REQUEST

The four extract_* methods allow the extraction of a subset of a superglobal array by providing a list of array keys. Keys that don't exist in the superglobal are ignored. If a second argument, an array of defaults, is supplied, then the result of extracting from the superglobal array is merged with it before being returned.

// Example: request for /index.php?f1=foo&f2=bar&f3=baz
$a = $request->extract_GET(array('f1', 'f3'));
// $a = array('f1' => 'foo', 'f3' => 'baz')
$a = $request->extract_GET(array('f2', 'xy'));
// $a = array('f2' => 'bar')
$a = $request->extract_GET(array('f2', 'f4'), array('f2' => 'a', 'f4' => 'b'));
// $a = array('f2' => 'bar', 'f4' => 'b')

get_uri

A call to get_uri returns the current request URI, without leading or trailing slashes. The uri_protocol (and possibly uri_get_variable) variable is used to determine the method used to work out what the real request URI was, since any application using the front controller pattern will rewrite the request but depend on the original request URI. There is no overhead to multiple calls to get_uri—the first time it is called, the result is cached so that future calls do not need to do any further processing.

get_user_agent

Returns the client user-agent string. Currently does nothing more than return the value of $_SERVER['HTTP_USER_AGENT'].

get_ip_address

Return the client IP address. Currently returns the value of $_SERVER['REMOTE_ADDR'], but possibly should take X-Forwarded-For header into account to handle clients behind proxies in a nicer way. (See #26.)

get_method, is_method

Calling get_method returns the HTTP request method, in uppercase (i.e. GET or POST). Calling is_method with a method as an argument will return true if the supplied method matches the request method (case-insensitive).

is_secure

Returns true if the current request was served over HTTPS, otherwise returns false.

1) Changing this to false is fine as long as no other library is going to perform it's own mangling. For example, HTML_QuickForm does its own magic_quotes_gpc fixing and therefore needs the raw inputs to be preserved.
csf/doc/request/root.txt · Last modified: 2009/09/20 17:01 by alan
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki