Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get URL query string parameters

What is the "less code needed" way to get parameters from a URL query string which is formatted like the following?

www.mysite.com/category/subcategory?myqueryhash

Output should be: myqueryhash

I am aware of this approach:

www.mysite.com/category/subcategory?q=myquery  <?php    echo $_GET['q'];  //Output: myquery ?> 
like image 540
enloz Avatar asked Dec 12 '11 03:12

enloz


People also ask

What are query string parameters in URL?

What are query string parameters? Query string parameters are extensions of a website's base Uniform Resource Locator (URL) loaded by a web browser or client application. Originally query strings were used to record the content of an HTML form or web form on a given page.

Does URL include query parameters?

Both parameters and query string contain key-value pairs. In a POST request, parameters can appear in the URL itself, but also in the datastream (as known as content). Query string is always a part of the URL. Parameters can be buried in form-data datastream when using POST method so they may not appear in the URL.

Does URL include query string?

A query string is a part of a uniform resource locator (URL) that assigns values to specified parameters.


2 Answers

$_SERVER['QUERY_STRING'] contains the data that you are looking for.


DOCUMENTATION

  • php.net: $_SERVER - Manual
like image 87
Filip Roséen - refp Avatar answered Nov 22 '22 10:11

Filip Roséen - refp


The PHP way to do it is using the function parse_url, which parses a URL and return its components. Including the query string.

Example:

$url = 'www.mysite.com/category/subcategory?myqueryhash'; echo parse_url($url, PHP_URL_QUERY); # output "myqueryhash" 

Full documentation here

like image 30
medina Avatar answered Nov 22 '22 08:11

medina