0

这是我的代码...

if($_SERVER['REQUEST_URI'] == '/shop/category/handheld-thermal-imaging/')
{
echo
'<div class="current-mark"></div><div class="top-level"><a href="/shop/category/handheld-thermal-imaging/">HANDHELD<br />THERMAL IMAGING</a></div>';
} 

else if($_SERVER['REQUEST_URI'] == '/shop/category/mobile-imaging/')
{
echo
'<div class="current-mark"></div><div class="top-level"><a href="/shop/category/mobile-imaging/">MOBILE IMAGING</a></div>';
}

基本上,此代码会根据您在站点中的哪个页面显示不同的左侧站点导航。它使用 PHP REQUEST_URI 功能通过页面的 URL 检测您所在的页面。

我的问题是,如何让代码检测同一个导航片段的多个 URL?

我试过这个代码......

if($_SERVER['REQUEST_URI'] == '/shop/category/handheld-thermal-imaging/' , '/shop/iphone-thermal-imaging-adapter/')
{

但是该代码似乎不起作用。基本上我在这里想弄清楚的是如何为 REQUEST_URI 'if' 语句使用多个 URL。提前致谢!

4

4 回答 4

2

将 URL 放入数组中,然后... if(in_array($_SERVER['REQUEST_URI'],$array))

于 2012-05-11T14:44:03.730 回答
1

您应该...切换到一个switch更简洁的语句,并提供此选项:

switch($_SERVER['REQUEST_URI']) {
    case '/shop/category/handheld-thermal-imaging/':
        // THERE IS NO CODE AT ALL HERE!
        // The absence of a "break;" causes control to "fall through" to the next
    case '/shop/iphone-thermal-imaging-adapter/':
        echo "something common for the thermal imaging urls";
        break;
    case 'some other url':
        echo "something for the url that stands alone";
        break;
}
于 2012-05-11T14:40:54.770 回答
0

您可以在条件中使用“OR”语句:

if($_SERVER['REQUEST_URI'] == "whatever" || $_SERVER['REQUEST_URI'] == 'something else")
{

}
于 2012-05-11T14:40:56.777 回答
0

我使用 OR 语句进行多个 URL 检查

<?php 

   if ((strpos($_SERVER['REQUEST_URI'], 'contact.php')  || strpos($_SERVER['REQUEST_URI'], 'register.php') || strpos($_SERVER['REQUEST_URI'], 'login.php')) === false) { 
     echo "show";  
   } else { 
     echo "hide";  
   } 

?>
于 2019-03-31T23:08:46.363 回答