Jet\Http_Request
This class is used to handle HTTP requests. Most importantly, this class is used to operate on input data (POST and GET).
Jet does this by default by making the superglobal variables $_POST, $_GET and $_REQUEST unavailable. Attempting to use these fields ends up throwing an exception (unless configuration specifies otherwise). The goal is to slightly improve the elementary security of the application and also to unify the handling of input here. We'll cover this separately, but first let's see what this class can do.
Method | Meaning of |
---|---|
public static initialize( ?bool $hide_PHP_request_data = null ): void |
Initialization is done by default in the script ~/application/Init/HTTPRequest.php. The method has a $hide_PHP_request_data parameter that determines whether it disables $_GET, $_POST and $_REQUEST, but the primary method for this setting is SysConf_Jet_Http::setHideRequest(). I recommend leaving this function enabled. |
public static GET( ): Data_Array |
Access to GET input data. See separate description below this table. |
public static POST( ): Data_Array |
Access to POST input data. See separate description below this table. |
public static currentURI( array $set_GET_params = [], array $unset_GET_params = [], ?string $set_anchor = null ): string |
It is used when you want to get the actual URL, but you just need the part without the domain. So if the current request URL is: https://some.domain/path/?param1=value1#anch then the method will return: /path/?param1=value1#anch. Last but not least, this method can affect GET parameters. For example, you need the current address, but you want it without the param1 GET parameter:
Http_Request::currentURI(unset_GET_params: ['param1']);
Or if you want to add a parameter:
Http_Request::currentURI(set_GET_params: ['new_param'=>'value']);
|
public static currentURL( array $set_GET_params = [], array $unset_GET_params = [], ?string $set_anchor = null ): string |
The method is identical to currentURI. The only difference is that it returns the entire URL - including the domain and the schema. |
public static URL( bool $include_query_string = true, bool $force_SSL = false ): string |
Returns the current complete URL. If include_query_string = true, then including all GET parameters. The method can also force the returned URL to be https even if the current request is only http. |
public static baseURL( bool $force_SSL = false ): string |
Returns the current base URL. This means only the schema and domain - no path and GET parameters. This method can also force https even if the current request is only http. |
public static schema( ): string |
Returns http or https. |
public static rawPostData( ): string |
Returns POST data read directly from the input. Designed for REST API server implementation and so on. |
public static requestMethod( ): string |
Returns GET, POST, PUT, DELETE, HEAD, CONNECT, OPTIONS, or PATCH. |
public static isHttp( ): bool |
Indicates whether the request is over plain http. |
public static isHttps( ): bool |
Indicates whether the request is over https. |
public static clientIP( ): string |
It combines several ways to find out the IP address of the client and returns this value. |
public static clientUserAgent( ): string |
Returns the user agent of the client. |
public static headers( ): array |
Returns HTTP request headers in the form of an associated array. |
public static postMaxSizeExceeded( ): bool |
Indicates whether the request has exceeded the maximum allowed POST and upload file size. Useful and important just for processing file uploads - allows you to alert the user to this problem. |
Práce s POST a GET daty (s daty na vstupu)
The attentive eye of the reader will not miss that the POST() and GET() methods return an instance of the Jet\Data_Array class, which is of course described in another part of the documentation. But since the topic of working with inputs is really important, we'll show the practical usage here. And we'll take it right through the practical examples.
Is there a given input parameter?
if(Http_Request::GET()->exists('my_param')) {
//Yes, it exists ...
}
if(Http_Request::POST()->exists('my_param')) {
//Yes, it exists ...
}
The input should be an integer and if the parameter is not present, I want the default value:
$GET = Http_Request::GET();
$id = $GET->getInt('id');
$value = $GET->getInt('value', 1234);
$value = Http_Request::POST()->getInt('value', 4321);
The input should be a decimal number and if the parameter is not present, I want the default value:
$value = Http_Request::POST()->getInt('value', 3.14);
I want true / false from the input
Http_Request::GET()->getBool('yes_no');
There is a string (!) on the input, but I prefer to treat it for safety (!):
Http_Request::POST()->getString('my_string_param')
The input is a string, but it must have one of the valid values. Once the input is unknown, I want the default value:
$action = Http_Request::GET()->getString(
key:'action',
default_value: 'main_action',
valid_values: ['main_action', 'action2', 'action3', 'action4']
);
I want the input in its raw form - without any interventions (and I know why I do it :-) )
(This can be data from article and description editors, for example, where HTML is directly assumed, and so on)
$raw_value = Http_Request::POST()->getRaw('param');
Access to inputs in the form of fields
Of course, the input can also be a multidimensional array. The standard way to handle such input in PHP is as follows:
$value = $_POST['some']['array']['value'];
(of course, that's only part of the job of getting input...)
How to do the same via Jet and have the detection of the existence of the given values in the array, the possible default value and the overwriting or the security of the string handled? It's simple. You use the exact same set of methods listed above. Only the key of the data you require is written differently. So the example above would look like this:
$value = Http_Request::POST()->getInt('/some/array/value');
The reality is that both Http_Request::POST() and Http_Request::GET() internally work with the Jet\Data_Array class, which is designed to do this array work.