Check for current URI - and include if NOT matched

Discussion in 'PHP' started by espmartin, Nov 12, 2007.

  1. #1
    Hello,

    I've been using this bit of PHP code within my "sidebar.php" file to insert
    menu items (external links) depending on what URI is viewed, and and it
    works great so far:

    <?php
      $homepage = "/";
      $currentpage = $_SERVER['REQUEST_URI'];
      if($homepage==$currentpage) {
      include('homepage-only-stuff.php');
      }
    ?>
    <?php
      $antivirus = "/anti-virus/";
      $antivirussub = "/anti-virus/anti-virus-articles.php";
      $currentpage = $_SERVER['REQUEST_URI'];
      if(($antivirus==$currentpage) || ($antivirussub==$currentpage)) {
      include('antivirus-only-stuff.php');
      }
    ?>
    
    PHP:
    ETC... (you get the picture :D )

    But now I want to exclude my blog pages. I can if I use:

     <?php
      $homepage = "/weblog/";
      $currentpage = $_SERVER['REQUEST_URI'];
      if($homepage!=$currentpage) {
      include('non-blog-stuff.php');
      }
    ?>
    
    PHP:
    But that does not match all the specific post pages, nor do I want to
    hard-code every post URI I make anyway :eek:

    So, how can I check for /weblog/AnyPostPage-URI/ (as a "wildcard" kind of
    match?) and exclude them?
     
    espmartin, Nov 12, 2007 IP
  2. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this

    <?php
      $homepage = "/weblog/";
      $currentpage = $_SERVER['REQUEST_URI'];
      if(strpos($currentpage, $homepage) === FALSE) {
      include('non-blog-stuff.php');
      }
    ?>
    PHP:
     
    tonybogs, Nov 13, 2007 IP
    espmartin likes this.
  3. espmartin

    espmartin Well-Known Member

    Messages:
    1,137
    Likes Received:
    46
    Best Answers:
    0
    Trophy Points:
    160
    #3
    Tonybogs,

    You rock! That works perfectly. Finding that needle in the haystack makes
    perfect sense. Can I ask why the triple "if equal" (===) instead of == ?

    PS: You get some cool green rep and iTrade for this :)

    PSS: I was able to green rep you, but I get an error every time I try the
    process of iTrading... :(
     
    espmartin, Nov 13, 2007 IP
  4. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The === FALSE means an absolute match. The problem being 0 also == FALSE

    So if the position of /weblog/ is 0, which it will be as its the first part of the URI the outcome of strpos($currentpage, $homepage) will be 0 and 0 == FALSE so youll be including the file everytime.

    I hope that is clear.

    I appreciate the rep, no worries about the iTrade just happy to help out :)

    Good Luck with your site
     
    tonybogs, Nov 13, 2007 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #5
    I think itrader only works in the BST forums ...
     
    krakjoe, Nov 13, 2007 IP