title: How can I check if a relative URL is the current one in PHP?
body: <p>I'm looking to make a PHP function that takes in a relative URL and returns whether that is this URL.</p>
  
  <pre><code>&lt;?PHP
  function isCurrentPage($page)
  {
      //Magic
  }
  ?&gt;
  </code></pre>
  
  <p>this will be passed values such as <code>"/"</code>, <code>"/foo/bar"</code>, <code>page.php</code>, or even <code>"foo/page.php?parameter=value"</code>.</p>
  
  <p>My first attempt involved using <code>$page == $_SERVER["REQUEST_URI"]</code>, but that says <code>"/foo/bar" != "/foo/bar/"</code>. That isn't much of an issue, but the difficulty comes with it saying <code>"/foo/bar" != "/foo/bar/index.php?parameter=value"</code>. For my purposes, I need it to say that these are equivalent.</p>
  
  <p><strong>How can I tell if the current URL is one passed to this function, with the given restrictions?</strong> I would prefer a simple, robust solution that is guaranteed to work for the next 5 years, as this is for a long-term, high-use project. Old, non-deprecated functions andor regexes are preferable.</p>
  
  <p>To synopsize, the method must return <code>true</code> when on the url <code>http://example.com/foo/bar</code>:</p>
  
  <ul>
  <li><code>isCurrentPage("http://example.com/foo/bar")</code></li>
  <li><code>isCurrentPage("http://example.com/foo/bar/")</code></li>
  <li><code>isCurrentPage("http://example.com/foo/bar/index.php")</code></li>
  <li><code>isCurrentPage("http://example.com/foo/bar/index.phps")</code></li>
  <li><code>isCurrentPage("http://example.com/foo/bar/index.phtml")</code></li>
  <li><code>isCurrentPage("/foo/bar")</code></li>
  <li><code>isCurrentPage("/foo/bar/")</code></li>
  <li><code>isCurrentPage("/foo/bar/index.php")</code></li>
  <li><code>isCurrentPage("/foo/bar?parameter=value")</code></li>
  <li><code>isCurrentPage("/foo/bar/?parameter=value")</code></li>
  <li><code>isCurrentPage("/foo/bar/index.php?parameter=value")</code></li>
  <li><code>isCurrentPage("/foo/bar/index.php#id")</code></li>
  <li><code>isCurrentPage("#id")</code></li>
  <li><code>isCurrentPage("index.php")</code></li>
  <li><code>isCurrentPage("index.php?parameter=value")</code></li>
  </ul>
  
  <p>et cetera.</p>