我正在尝试用PHP检查用户是否在某些特定页面上。目前我是这样做的:
<?php if($_SERVER['REQUEST_URI'] == "/forum/"){echo "class='active'";} ?>
尽管如此,只有当URL是http://example.com/forum/
时,这才有效
我如何使上面的代码在论坛和/forum/??
上都能工作
示例:
http://example.com/forum/anotherpage
您可以使用startswith function
(请参阅此stackoverflow post:startsWith()和endsWith)函数。在这里,你的测试将是:
if (startsWith ($_SERVER['REQUEST_URI'], '/forum/'))
您也可以使用正则表达式:http://php.net/manual/en/regex.examples.php.在这里,你的测试将是:
if (preg_match ('#^/forum/#', $_SERVER['REQUEST_URI']))
希望这能有所帮助。