If Else with URL

Discussion in 'PHP' started by Hades, Oct 25, 2008.

  1. #1
    Hi Guys,

    Can anyone help me out by telling me how I can display something for index.php and display something else for all different pages?

    I know how to do the normal if else statements, but I don't understand it when variables are put it, especially when you have to request the url.

    I will appreciate any help I can get.

    Regards,
    Nick
     
    Hades, Oct 25, 2008 IP
  2. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    create an index.php file with different content than the other files

    holy shit I should become a rocket scientist
     
    Kyosys, Oct 25, 2008 IP
  3. Calon

    Calon Peon

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    [​IMG]
     
    Calon, Oct 25, 2008 IP
  4. Hades

    Hades Well-Known Member

    Messages:
    1,873
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    150
    #4
    I guess that's why we have so many problems going into space; cause they hire people like you.

    I just put index.html as one of the files. I am gonna do this for about 15 other pages, so I didn't think anyone would answer this if they had to type so many lines.

    Now, if you have something more constructive, I would appreciate it. Otherwise, let the non-rocket scientists answer this. OK?

    Regards,
    Nick
     
    Hades, Oct 25, 2008 IP
  5. IGiveMoney

    IGiveMoney Peon

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Nick

    Non rocket scientist here offering something like this:

    
    $loc = $_SERVER['PHP_SELF'];
    
    # Then do something like:
    
    if($loc != 'index.php'){
     include('files/other.file.html');
    }
    else {
     include('files/index.html');
    }
    
    
    PHP:
    If you wanted to take it a step further then you could
    build an array of your pages and if it doesn't include one
    of those pages - simply give them the index.html page.
     
    IGiveMoney, Oct 25, 2008 IP
    Hades likes this.
  6. Hades

    Hades Well-Known Member

    Messages:
    1,873
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    150
    #6
    IGiveMoney: Thank you very much. I haven't used it yet, but It looks like what I need. How would i go about building the array?

    Regards,
    Nick
     
    Hades, Oct 25, 2008 IP
  7. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Dude, seriously, www.php.net/array. Can't be that hard.
     
    Icheb, Oct 25, 2008 IP
  8. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You might want to look into mod_rewrite then
     
    Kyosys, Oct 26, 2008 IP
  9. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #9
    This is what I usually use sometimes:

    
    <?php
    $allowedPages = array('test','game','weather');
    //$_GET['page'] contains page name like TEST, Test or test
    $page = strtolower($_GET['page']); 
    if(in_array($page,$allowedPages)){
        include($page.'.php');
    } else {
       include('main.php');
    }
    ?>
    
    PHP:
    Peace,
     
    Barti1987, Oct 26, 2008 IP
  10. RaulGonzales

    RaulGonzales Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Yes good thank you
     
    RaulGonzales, Oct 27, 2008 IP