Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I check if a relative URL is the current one in PHP?
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload