PHP - Parse Current Url

PHP - parse current URL

You can use parse_url();

$url = 'http://www.mydomain.com/abc/';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);

which would give you

Array
(
[scheme] => http
[host] => www.mydomain.com
[path] => /abc/
)
/abc/

Update: to get current page url and then parse it:

function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}

print_r(parse_url(curPageURL()));

echo parse_url($url, PHP_URL_PATH);

source for curPageURL function

PHP Parse URL From Current URL

Is not a really clean solution, but you can try something like:

$url = "MYURL";
$parse = parse_url($url);
parse_str($parse['query']);

echo $referredby; // same name of the get param (see parse_str doc)


PHP.net: Warning

Using this function without the result parameter is highly DISCOURAGED and DEPRECATED as of PHP 7.2.

Dynamically setting variables in function's scope suffers from exactly same problems as register_globals.

Read section on security of Using Register Globals explaining why it is dangerous.

Get current URL path with query string in PHP

You want $_SERVER['REQUEST_URI']. From the docs:

'REQUEST_URI'


The URI which was given in order to access this page; for instance, '/index.html'.

PHP: Parse Current URL

If you do this...

$array = explode('/', $_SERVER['REQUEST_URI']);

your array should look like this...

echo $array [0]; // mysite
echo $array [1]; // project
echo $array [2]; // detail
echo $array [3]; // fp
echo $array [4]; // 3

How to get the full current URL in PHP with the query string and parse it?

try this instead:

<?php
$br = "<br>";
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'https') === FALSE ? 'http' : 'https';
$hostame = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
if ($params == ""){
$currentUrl = $protocol . '://' . $hostame . $script . $params;
}
else {
$currentUrl = $protocol . '://' . $hostame . $script . '?' . $params;
}
?>

URL: <?php echo $currentUrl . $br; ?>
Protocol: <?php echo $protocol . $br; ?>
Hostname: <?php echo $hostame . $br; ?>
<?php
if ($script == ""){
exit;
} else {
echo "Page / Script: " . $script . $br; };
if ($params == ""){
exit;
} else {
echo "Params: " . $params . $br; };
?>

PHP: Best way to parse URL query without variable name?

If you are sure that nothing malicious would be passed (or you are just testing), then you could do something like:

$output = eval('return ' . parse_url($_SERVER['REQUEST_URI'])['query'] . ';');

echo $output;

Obligatory notice:

Caution
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

However, I would advise you use the following method and parse the expression with parser in the linked question:

$exp = parse_url($_SERVER['REQUEST_URI'])['query']; // 1+1

Reading Material

How to evaluate formula passed as string in PHP?

parse_url

eval

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

How do I parse a URL in PHP?

You can use parse_url to get down to the hostname.

e.g.:

$url = "http://localhost/path/to/?here=there";
$data = parse_url($url);
var_dump($data);

/*
Array
(
[scheme] => http
[host] => localhost
[path] => /path/to/
[query] => here=there
)
*/


Related Topics



Leave a reply



Submit