【问题标题】:Get current URL path with query string in PHP [duplicate]在PHP中获取带有查询字符串的当前URL路径[重复]
【发布时间】:2013-04-18 09:43:57
【问题描述】:

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

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

我想要这个:

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

【问题讨论】:

  • 您同时要求路径和查询字符串,但是搜索如何找到路径的人最终会出现在这里。你能把标题改成“在 PHP 中使用查询字符串获取当前 URL 路径”吗?

标签: php url


【解决方案1】:
<?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
}
?>

【讨论】:

  • 您可能会提供一些解释,说明已接受答案中的单行不这样做。
【解决方案2】:

你想要$_SERVER['REQUEST_URI']。来自the docs

'REQUEST_URI'

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

【讨论】:

    【解决方案3】:

    应该是:

    $_SERVER['REQUEST_URI'];
    

    看看:Get the full URL in PHP

    【讨论】:

      猜你喜欢
      • 2011-07-10
      • 1970-01-01
      • 2016-12-31
      • 2012-08-19
      • 2018-02-14
      • 1970-01-01
      • 2015-10-11
      • 2018-09-04
      • 2013-11-21
      相关资源
      最近更新 更多