I am trying to check with PHP if the user is on some specific pages. Currently I am doing it like this:
<?php if($_SERVER['REQUEST_URI'] == "/forum/"){echo "class='active'";} ?>
Although, this only works if the URL is http://example.com/forum/
How can I make the above code works, on both /forum/ but also on /forum/??
Example:
http://example.com/forum/anotherpage
You can use a startswith function
(see this stackoverflow post: startsWith() and endsWith() functions in PHP). Here, you test would be:
if (startsWith ($_SERVER['REQUEST_URI'], '/forum/'))
You can also use a regexp: http://php.net/manual/en/regex.examples.php. Here, your test would be:
if (preg_match ('#^/forum/#', $_SERVER['REQUEST_URI']))
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With