Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to check total no. parameters in URL?

i am retrieving the parameters using $_REQUEST. Is there a way of finding total no. of parameters in URL instead of retrieving each one and then counting ?

like image 916
Reena Parekh Avatar asked Jan 18 '13 11:01

Reena Parekh


People also ask

How many parameters does a URL have?

1000 parameters is the maximum by default. This default can be customized in the HTTP Protocol Validation Policy. An attacker would use exceptionally long parameter names or values for three different purposes: To launch an overflow attack against the data structure that stores the parameters as name-value pairs.

How do I find parameters in URL?

For getting the URL parameters, there are 2 ways: By using the URLSearchParams Object. By using Separating and accessing each parameter pair.

How do I fix too many URL parameters?

How To Fix. There are several things that you can do to avoid using too many URL parameters: Minimize the number of parameters by eliminating duplicate, unnecessary, or empty parameters from the URL. Use server-side URL rewrites to convert them into static, human-readable URLs.

How can I get params in PHP?

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it.


2 Answers

This will give you the total number of & separated URL query parameters:

count(explode('&', $_SERVER['QUERY_STRING']))

If you only want unique parameters, use $_GET instead:

count($_GET)
like image 187
Gumbo Avatar answered Oct 12 '22 23:10

Gumbo


Retrieve them with $_GET. This should be enough.

Example:

// url: index.php?a=1&b=2&c=3
echo count($_GET); // 3 params, $_GET['a'], $_GET['b'], $_GET['c']

Note: you can also pass arrays in url ( check here ), and the whole array is counted once.

like image 41
Vlad Preda Avatar answered Oct 13 '22 00:10

Vlad Preda