在 PHP 中获取当前 URL 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16198790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:42:25  来源:igfitidea点击:

Get current URL path in PHP

phpurl

提问by totallyuneekname

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

我需要从当前请求的 URL 中获取路径。例如,如果当前 URL 是:

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

I would want this:

我想要这个:

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

回答by Adrian

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

你要$_SERVER['REQUEST_URI']。从文档

'REQUEST_URI'

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

'REQUEST_URI'

为访问此页面而提供的 URI;例如,'/index.html'

回答by Mehdi Karamosly

it should be :

它应该是 :

$_SERVER['REQUEST_URI'];

Take a look at : Get the full URL in PHP

看一看:Get the full URL in PHP

回答by manip5595

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