Can I write an if/else statement depending on the page url?

Discussion in 'PHP' started by tyler_durden, May 11, 2009.

  1. #1
    I have a space in a global field that I want to be different on the homepage, so my thoughts were to create an if/else statement.

    if page = http://www.homepage
    echo = 'Statement 1'
    else
    echo = 'statement 2'

    That's an idea of what i'm looking for, but i'm a php novice. Could someone help me with this coding? Thanks!
     
    tyler_durden, May 11, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You can use something like this:

    
    
    switch($_SERVER['PHP_SELF']):
    
    case '/index.php':
    
    echo 'SOMETHING ON THE INDEX PAGE';
    
    break;
    
    default:
    
    echo 'ANY OTHER PAGE';
    
    endswitch;
    
    
    PHP:
    This may not work as expected if you're using wordpress or another application where every page request is funnelled through the index.php page.
     
    jestep, May 11, 2009 IP
  3. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I should have mentioned i'm using a cms, and I have many mods with url rewrites, etc. which is why i was hoping I could just use the simple url.
     
    tyler_durden, May 11, 2009 IP
  4. monster64

    monster64 Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I believe $_SERVER['REQUEST_URI'] is what you are looking for.
     
    monster64, May 11, 2009 IP
  5. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'll play around with that. Thanks!
     
    tyler_durden, May 11, 2009 IP
  6. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok, when I use this code:
    <?php
    echo $_SERVER['REQUEST_URI'];
    ?>
    Code (markup):
    I get the output of "/" on my homepage. Can I still use that in a statement, or does it have to be a full directory name, etc?

    And i'm still unsure of how to exactly code this. I can echo $_SERVER['REQUEST_URI']; , but how do I use it in an if/else statement? It's getting to be near the end of the day, and I may just have to study some php tomorrow.
     
    tyler_durden, May 11, 2009 IP
  7. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Eureka! I got it. Thanks for everyone's help. i didn't think it would be that easy!
     
    tyler_durden, May 11, 2009 IP
  8. monster64

    monster64 Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Glad you got it to work. Just to clarify, most of the time $_SERVER['REQUEST_URI'] retrieves everything past the URL itself. However, some browsers or applications may send the full URI including the URL, so you should watch out for that.
     
    monster64, May 11, 2009 IP
  9. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for that clarification. I better test this in a few browsers. What about search engines/spiders? That's what I'm doing this for the most, although it will benefit the viewer as well.
     
    tyler_durden, May 11, 2009 IP