Core\Request::queryParameter
Core\Request::queryParameter(string $name): ?tainted stringCore\Request::queryParameter( string $name, ): ?tainted string
Returns one parameter from the query string as text. The query string is the part of the address after the ?, such as q=shoes&page=2. You give the name of the parameter. The result is its value as a tainted string, or null when the address has no parameter of that name. Tainted text came from the client, so Novis does not allow it in an SQL query or in HTML until you escape or check it.
The name must match exactly, and upper and lower case are different. A + in the value is returned as a space, and a percent escape such as %21 is decoded.
A name with square brackets, such as tag[]=red, gives null. Read those values with Core\Request::queryParameters.
To get a number, convert the text with as, for example as ?int. A command-line program has no request, so queryParameter throws a LogicError there. When an escape in the query string is not valid UTF-8 text, queryParameter throws a RuntimeError.
The examples below show a search page, a parameter that is missing or is not a number, and a name with brackets.
Parameters
$namestringThe parameter's name, decoded. Upper and lower case are different.
Return value
Returns ?tainted string. The parameter's value as a tainted string, or null when the query has no parameter with this name. A name with square brackets, such as tag[]=a, also gives null. Read those values with queryParameters. Use as to convert the text, for example as ?int.
Errors
LogicErrorThis program is not handling a request.RuntimeErrorThe query string has a percent escape that decodes to bytes that are not UTF-8.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Request::queryParameter, queryParameters and queryParametersAs read the query string. formField, formFields and formFieldsAs read a submitted form. The old names were query, queryArray, queryAs, post, postArray and postAs. |
Examples
Reading a search term
<?nvs// Shows how to read the parameters of an address such as `/search?q=red+shoes&page=2`.
// `Core\Request::queryParameter` returns the value of the parameter "q" as text.// A `+` in the address is a space, so this prints "search: red shoes".?tainted string $search = Core\Request::queryParameter('q');echo 'search: ', $search ?? '', "\n";
// The value of "page" is the text "2". `as int` converts it to a whole number.// This prints "next page: 3".int $page = Core\Request::queryParameter('page') as int;echo 'next page: ', $page + 1, "\n";search: red shoesnext page: 3A parameter that was not sent
<?nvs// Shows what `Core\Request::queryParameter` returns for a parameter that is missing or is not a number.
// The address has no parameter named "sort", so the result is `null`.// `??` gives the default value "newest" instead. This prints "sort: newest".echo 'sort: ', Core\Request::queryParameter('sort') ?? 'newest', "\n";
// The address has a parameter named "page", but none named "Page".// Upper and lower case are different, so this result is `null` too.echo Core\Request::queryParameter('Page') == null ? "no Page\n" : "Page found\n";
// The value of "page" is "abc", which is not a whole number.// `as ?int` gives `null` for it. This prints "page 1".?int $page = Core\Request::queryParameter('page') as ?int;echo 'page ', $page ?? 1, "\n";sort: newestno Pagepage 1A name with brackets
<?nvs// Shows what `Core\Request::queryParameter` returns for a parameter written with square brackets.
// The address is `/products?tag[]=red&tag[]=new&sort=price`.// "sort" has no brackets, so this prints "sort: price".echo 'sort: ', Core\Request::queryParameter('sort') ?? 'none', "\n";
// "tag" was sent with brackets, so `queryParameter` returns `null` for it. This prints "tag: none".echo 'tag: ', Core\Request::queryParameter('tag') ?? 'none', "\n";
// `Core\Request::queryParameters` returns the values of "tag". This prints "tags: 2".echo 'tags: ', Core\Arr::count(Core\Request::queryParameters('tag')), "\n";sort: pricetag: nonetags: 2