If the URL is https://www.mohsin.com?main=17&street=71, then what is the PHP code to grab the "main" and "street" values?
Asked
Active
Viewed 1.4k times
1
-
`$_GET['main']` and `$_GET['street']` – Stuart Wagner Feb 08 '17 at 11:48
-
`$_GET['main']` and `$_GET['street']`? – Rakesh Sharma Feb 08 '17 at 11:48
-
don't forget filter values – Artem Ilchenko Feb 08 '17 at 11:49
3 Answers
8
Try with this :
<?php
$main = $_GET['main'];
$street = $_GET['street'];
?>

desertnaut
- 57,590
- 26
- 140
- 166

Camilo Go Jr.
- 3,184
- 2
- 11
- 18
5
Just call the following function:
function getQueryParameter($url, $param) {
$parsedUrl = parse_url($url);
if (array_key_exists('query', $parsedUrl)) {
parse_str($parsedUrl['query'], $queryParameters);
if (array_key_exists($param, $queryParameters)) {
return $queryParameters[$param];
}
}
}
Example:
$parameter = getQueryParameter('https://www.mohsin.com?main=17&street=71', 'main')
will return17
.$parameter = getQueryParameter('https://www.mohsin.com?main=17&street=71', 'street')
will return71
.$parameter = getQueryParameter('https://www.mohsin.com?main=17&street=71', 'invalid')
will returnnull
.

Spomky-Labs
- 15,473
- 5
- 40
- 64
1
$main = $_GET['main']
will get the main variable and $street = $_GET['street']
will get the street variable. All URL parameters are loaded into the PHP $_GET super global array and the super global is an associative array.

Chris Rutherfurd
- 1,617
- 1
- 15
- 32