150

I need to get the path with query string 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"
Community
  • 1
  • 1
totallyuneekname
  • 2,000
  • 4
  • 16
  • 27
  • 4
    You are asking for both the path and the query string together, but people who are googling for how to just find the path will end up here. Can you change the title to be "Get current URL path with query string in PHP"? – kloddant Apr 01 '21 at 16:58

3 Answers3

283

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

isherwood
  • 58,414
  • 16
  • 114
  • 157
Adrian
  • 42,911
  • 6
  • 107
  • 99
50

it should be :

$_SERVER['REQUEST_URI'];

Take a look at : Get the full URL in PHP

Community
  • 1
  • 1
Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50
7
<?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
}
?>
simhumileco
  • 31,877
  • 16
  • 137
  • 115
manip5595
  • 103
  • 1
  • 3
  • 4
    You might offer some explanation of what this does that the one-liner in the accepted answer doesn't. – isherwood Apr 03 '19 at 14:01