Get current URL path with query string in PHP

PhpUrl

Php Problem Overview


I need to get the path from the URL of the current request. For example, if the current URL is:

"http://www.example.com/example/test/hi.php?randomvariable=1"

I would want this:

"/example/test/hi.php?randomvariable=1"

Php Solutions


Solution 1 - 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'.

Solution 2 - Php

it should be :

$_SERVER['REQUEST_URI'];

Take a look at : https://stackoverflow.com/questions/6768793/php-get-the-full-url

Solution 3 - Php

<?php
function current_url()
{
    $url      = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $validURL = str_replace("&", "&amp;", $url);
    return $validURL;
}
//echo "page URL is : ".current_url();

$offer_url = current_url();

?>



<?php

if ($offer_url == "checking url name") {
?> <p> hi this is manip5595 </p>

<?php
}
?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontotallyuneeknameView Question on Stackoverflow
Solution 1 - PhpAdrianView Answer on Stackoverflow
Solution 2 - PhpMehdi KaramoslyView Answer on Stackoverflow
Solution 3 - Phpmanip5595View Answer on Stackoverflow