【问题标题】:How can I check if a relative URL is the current one in PHP?如何检查相对 URL 是否是 PHP 中的当前 URL?
【发布时间】:2013-04-23 06:32:48
【问题描述】:

我正在寻找一个 PHP 函数,它接受一个相对 URL 并返回是否是这个 URL。

<?PHP
function isCurrentPage($page)
{
    //Magic
}
?>

这将被传递值,例如"/""/foo/bar"page.php,甚至是"foo/page.php?parameter=value"

我的第一次尝试涉及使用$page == $_SERVER["REQUEST_URI"],但上面写的是"/foo/bar" != "/foo/bar/"。这不是什么大问题,但困难在于它说"/foo/bar" != "/foo/bar/index.php?parameter=value"。为了我的目的,我需要它说这些是等价的。

在给定的限制条件下,如何判断当前 URL 是否是传递给此函数的?我更喜欢一个简单、可靠的解决方案,该解决方案可以保证在未来 5 年内有效,因为这是一个长期的、高使用率的项目。旧的、未弃用的函数和/或正则表达式更可取。

概括地说,该方法必须在 URL http://example.com/foo/bar 上返回 true

  • isCurrentPage("http://example.com/foo/bar")
  • isCurrentPage("http://example.com/foo/bar/")
  • isCurrentPage("http://example.com/foo/bar/index.php")
  • isCurrentPage("http://example.com/foo/bar/index.phps")
  • isCurrentPage("http://example.com/foo/bar/index.phtml")
  • isCurrentPage("/foo/bar")
  • isCurrentPage("/foo/bar/")
  • isCurrentPage("/foo/bar/index.php")
  • isCurrentPage("/foo/bar?parameter=value")
  • isCurrentPage("/foo/bar/?parameter=value")
  • isCurrentPage("/foo/bar/index.php?parameter=value")
  • isCurrentPage("/foo/bar/index.php#id")
  • isCurrentPage("#id")
  • isCurrentPage("index.php")
  • isCurrentPage("index.php?parameter=value")

等等。

【问题讨论】:

  • 这个问题没有显示任何研究工作。 做好功课很重要。告诉我们您发现了什么以及为什么它不能满足您的需求。这表明您已经花时间尝试帮助自己,它使我们免于重复明显的答案,最重要的是它可以帮助您获得更具体和相关的答案。 FAQ.
  • 别名怎么样,例如阿帕奇Alias /baz /foo/bar?
  • @MarcB 目前不是问题,但如果你有解决方案,我很乐意看到!

标签: php url


【解决方案1】:

怎么样:

function isCurrentPage($page)
{
      //Magic
      $page = preg_replace('/https?:\/\/[a-zA-Z0-9_\.\-]+/', '', $page);

      $url = 'http';
      if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
          $url .= 's';
      }
      $url .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT']  . $page;
      $handle = curl_init($url);
      curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);

      /* Get the HTML or whatever is linked in $url. */
      $response = curl_exec($handle);

      /* Check for 404 (file not found). */
      $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
      curl_close($handle);

      return $httpCode != 404;
}

【讨论】:

  • $page"https://example.com/foo" 时,这会中断,因为第一行是$url "http://example.com:80https://example.com/foo"。 +1 用于错误检查!
  • 问题仍然存在,只是现在它使$url"https://example.com:80https://example.com/foo"
  • 太棒了,效果很好。不幸的是,@Lix 的解决方案更优雅,可能更面向未来,这也是我最终使用的解决方案。不过谢谢你的回答!我真的很喜欢使用正则表达式和错误检查
  • 不支持 URL 中的非 ASCII 字符。这可能是 90 年代的解决方案......
  • @Alph.Dev 你确定吗?看起来唯一的代码点限制是域名,它仍然是 ASCII-only。即使是花哨的新中文和俄文顶级域名也是真正的 ASCII 引擎盖(例如.рф is really .xn--p1ai)。
【解决方案2】:

您也许可以使用parse_url() function 来拆分您的 URL 并删除所有不重要的数据,例如查询字符串。

这是一个简单的例子:

$url = 'http://yoursite.com/foo/bar?some=param';
$urlParts = parse_url($url);
// Array
// (
//     [scheme] => http
//     [host] => yoursite.com
//     [path] => /foo/bar
//     [query] => ?some=param
// )

您现在可以将$urlParts['path'] 与您的已知路径列表进行比较...

【讨论】:

  • 有点不完整,因为我必须添加好几十行代码才能使其适用于上述情况,但这是我解决方案的手段。谢谢!
  • 哦...是的...对于我的大部分答案,我不提供复制粘贴解决方案,而是在正确的方向上轻推,并提供一些指向相关文档的有用链接...跨度>
  • 很高兴能帮上忙!编码愉快!
  • 一个好习惯!不过,当包含 URL 的 index.* 部分时,我遇到了麻烦。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-05
  • 2018-09-26
  • 2013-05-12
  • 2012-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多