DigitalSky
DigitalSky

Reputation: 23

How to use multiple URLs in PHP REQUEST_URI statement?

Here is my code...

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>';
}

Basically this code displays a different left side site navigation depending on which page you're on in the site. It detects what page you're on by the URL of the page by using the PHP REQUEST_URI feature.

My question is this, how can I make the code detect multiple URLs for the same navigation piece?

I tried this code...

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

But that code doesn't seem to work. Basically all I'm trying to figure out here is how I can use multiple URLs for the REQUEST_URI 'if' statement. Thanks in advance!

Upvotes: 0

Views: 1198

Answers (4)

Nisharg Shah
Nisharg Shah

Reputation: 19542

I used the OR statement for multiple URL checking

<?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";  
   } 

?>

Upvotes: 0

Sindhara
Sindhara

Reputation: 1473

Put the URLs in an array and then... if(in_array($_SERVER['REQUEST_URI'],$array))

Upvotes: 2

Jonathan Barlow
Jonathan Barlow

Reputation: 1075

You can use an "OR" statement in your conditional:

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

}

Upvotes: 0

Jon
Jon

Reputation: 437376

You should... switch to a switch statement, which is both neater and offers this option:

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;
}

Upvotes: 1

Related Questions