Get Url Parameter in PHP

How can I get parameters from a URL string?

You can use the parse_url() and parse_str() for that.

$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['email'];

If you want to get the $url dynamically with PHP, take a look at this question:

Get the full URL in PHP

GET URL parameter in PHP

$_GET is not a function or language construct—it's just a variable (an array). Try:

<?php
echo $_GET['link'];

In particular, it's a superglobal: a built-in variable that's populated by PHP and is available in all scopes (you can use it from inside a function without the global keyword).

Since the variable might not exist, you could (and should) ensure your code does not trigger notices with:

<?php
if (isset($_GET['link'])) {
echo $_GET['link'];
} else {
// Fallback behaviour goes here
}

Alternatively, if you want to skip manual index checks and maybe add further validations you can use the filter extension:

<?php
echo filter_input(INPUT_GET, 'link', FILTER_SANITIZE_URL);

Last but not least, you can use the null coalescing operator (available since PHP/7.0) to handle missing parameters:

echo $_GET['link'] ?? 'Fallback value';

PHP get URL string parameters?

Since you are only interested in manipulating the path component you can simply use parse_url to extract the path component instead of regex:

function generateurl($url, $lc) {
$parts = parse_url($url);
$parts['path'] = preg_replace('@^/[a-z][a-z]/@', '/', $parts['path']);
if ($lc !== 'en') {
$parts['path'] = '/' . $lc . $parts['path'];
}
$url = '';
if (isset($parts['scheme'])) $url .= $parts['scheme'] . '://';
if (isset($parts['host'])) $url .= $parts['host'];
if (isset($parts['port'])) $url .= ':' . $parts['port'];
if (isset($parts['path'])) $url .= $parts['path'];
if (isset($parts['query'])) $url .= '?' . $parts['query'];
if (isset($parts['fragment'])) $url .= '#' . $parts['fragment'];
return $url;
}

The above function could be simplified if you have http_build_url function available. Function input and output:

                           https://mywebsite.com/ + en = https://mywebsite.com/
https://mywebsite.com/ + al = https://mywebsite.com/al/
https://mywebsite.com/ + it = https://mywebsite.com/it/
https://mywebsite.com/al/ + en = https://mywebsite.com/
https://mywebsite.com/al/ + al = https://mywebsite.com/al/
https://mywebsite.com/al/ + it = https://mywebsite.com/it/

https://mywebsite.com/videos/ + en = https://mywebsite.com/videos/
https://mywebsite.com/videos/ + al = https://mywebsite.com/al/videos/
https://mywebsite.com/videos/ + it = https://mywebsite.com/it/videos/
https://mywebsite.com/al/videos/ + en = https://mywebsite.com/videos/
https://mywebsite.com/al/videos/ + al = https://mywebsite.com/al/videos/
https://mywebsite.com/al/videos/ + it = https://mywebsite.com/it/videos/

https://mywebsite.com/videos/search/?q=YouTube + en = https://mywebsite.com/videos/search/?q=YouTube
https://mywebsite.com/videos/search/?q=YouTube + al = https://mywebsite.com/al/videos/search/?q=YouTube
https://mywebsite.com/videos/search/?q=YouTube + it = https://mywebsite.com/it/videos/search/?q=YouTube
https://mywebsite.com/al/videos/search/?q=YouTube + en = https://mywebsite.com/videos/search/?q=YouTube
https://mywebsite.com/al/videos/search/?q=YouTube + al = https://mywebsite.com/al/videos/search/?q=YouTube
https://mywebsite.com/al/videos/search/?q=YouTube + it = https://mywebsite.com/it/videos/search/?q=YouTube

/videos/search/?q=YouTube + en = /videos/search/?q=YouTube
/videos/search/?q=YouTube + al = /al/videos/search/?q=YouTube
/videos/search/?q=YouTube + it = /it/videos/search/?q=YouTube
/al/videos/search/?q=YouTube + en = /videos/search/?q=YouTube
/al/videos/search/?q=YouTube + al = /al/videos/search/?q=YouTube
/al/videos/search/?q=YouTube + it = /it/videos/search/?q=YouTube

PHP - How to get $_GET Parameter without value

Based on your example, simply exploding the = might quickly suits your need.

$url = $_SERVER['REQUEST_URI'];
$parse_url = parse_url($url, PHP_URL_QUERY);
$queryparam = explode("=", $parse_url);
echo $queryparam[0];
/* OUTPUT query */

if (in_array($queryparam[0], $array_of_params)){ ... }

But you can simply achieve the same thing like this:

if (@$_GET["query"] != ""){ ... }

PHP Get URL with Parameter

basename($_SERVER['REQUEST_URI']);

This will return all URLs with page name. (e.g.: index.php?id=1&name=rr&class=10).

PHP Get URL Parameter and its Value

Why not using $_GET global variable?

foreach($_GET as $key => $value)
{
// do your thing.
}

Get URL Parameter in PHP using slash

You must use absolute URLs to load the css and js files.

Instead of relative URLs like:

../../path/to/cssFile.css

use absolute URLs:

/path/to/cssFile.css


Related Topics



Leave a reply



Submit