include file based on url

Discussion in 'PHP' started by jazzylee77, Nov 8, 2007.

  1. #1
    I'm wanting to show a different submenu file for different folders of a dynamic script. This is what I have so far, but doesn't work.

    <?php
    function curPageURL() {
     $pageURL = 'http';
     if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     $pageURL .= "://";
     if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
     } else {
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     }
     return $pageURL;
    }
    ?>
    
    <?php
      echo curPageURL();
    ?>
    
    <?php 
    if (curPageURL() = “http://www.mysite.com/tag/Sports") {
       include("Sports.php");
    } else {
       include("default.php");
    }
    ?>
    PHP:
    I had the echo curPageURL(); part in there just to test that it was getting pageurl ok and it worked till I added the bit afterward.
     
    jazzylee77, Nov 8, 2007 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    The equal sign should be a double equal sign:
    if (curPageURL() == "http://www.mysite.com/tag/Sports")
     
    phper, Nov 8, 2007 IP
  3. jazzylee77

    jazzylee77 Peon

    Messages:
    578
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I wasn't sure about the equal operator and had tried the double equal as well. So I've made that change back to the ==. Still doesn't work

    current code (other than mysite.com)

    <?php
    function curPageURL() {
     $pageURL = 'http';
     if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     $pageURL .= "://";
     if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
     } else {
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     }
     return $pageURL;
    }
    ?>
    
    <?php
      echo curPageURL();
    ?>
    
    <?php 
    
    
    if (curPageURL() == “http://www.mysite.com/tag/Sports") {
       include("test.php");
    } else {
       include("default.php");
    }
    ?>
    PHP:
     
    jazzylee77, Nov 8, 2007 IP
  4. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    The opening quote looks funny there. Just a wild guess, are you using MS Word to code? If so, open the file with Notepad and re-type the quote mark in the same line above (the one that enclosed http://www.mysite.com/tag/Sports).

    Otherwise, pasting the error messages that you get to here will help investigating the issue.
     
    phper, Nov 8, 2007 IP
    jazzylee77 likes this.
  5. jazzylee77

    jazzylee77 Peon

    Messages:
    578
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ah! Good eye! That was it. I had copied part of it from another file and the odd character sneaked in. Thanks.
     
    jazzylee77, Nov 8, 2007 IP
  6. jazzylee77

    jazzylee77 Peon

    Messages:
    578
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok now I could repeat that if else for all the different categories and sub menus. But maybe there is a more efficient way.
     
    jazzylee77, Nov 8, 2007 IP
  7. jazzylee77

    jazzylee77 Peon

    Messages:
    578
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #7
    using elseif this is working fine. Now how do I make the include work for subfolders too?

    if (curPageURL() == “http://www.mysite.com/tag/Sports") {
       include("sports.php");
    PHP:

    but also including sports.php for http://www.mysite.com/tag/Sports/Baseball http://www.mysite.com/tag/Sports/Baseball/Bats etc.
     
    jazzylee77, Nov 9, 2007 IP
  8. jazzylee77

    jazzylee77 Peon

    Messages:
    578
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ok I found that. Just change == to >=

    fun to watch me learn huh? ;)
     
    jazzylee77, Nov 9, 2007 IP