152

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"
1
  • 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"? Commented Apr 1, 2021 at 16:58

3 Answers 3

289

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

Sign up to request clarification or add additional context in comments.

Comments

53

it should be :

$_SERVER['REQUEST_URI'];

Take a look at : Get the full URL in PHP

Comments

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
}
?>

1 Comment

You might offer some explanation of what this does that the one-liner in the accepted answer doesn't.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.