PHP IF - ELSE codes based on URL

Discussion in 'PHP' started by Chri, Jun 23, 2010.

  1. #1
    Can some one help me out in this?

    I want to know the codes for a Php if, else condition tags based on URL.

    I want it like,

    if url=domain.com
    echo <div> dont show domain button<div>

    else

    echo <div> dont do nothing </div>


    and


    if url=something.domain.com
    echo <div> dont show something button <div>

    else

    echo <div> do nothing </div>


    I hope you got the thing from the Framework :)

    i.e When running on the domain url, i dont want to see domain link on the page in the nav bar i make.
    and when am in the subdomain, i dont want to see that subdomian link in nav bar. I can arrange the things for navbar. but not so sure about the PHP code.

    Can some one help me out and clear this giving me the code?

    also i would like a wildcard it the cases like if it is domain.com/* so it remain correct for all the pages in the blog :)
     
    Chri, Jun 23, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    <?php
    
    $url = str_replace("www.", "", $_SERVER['HTTP_HOST']);
    
    if ($url == "domain.com") {
    echo "<div>dont show domain button</div>";
    
    } else {
    
    echo "<div>dont do nothing</div>";
    
    }
    
    
    
    if ($url == "something.domain.com") {
    echo "<div>dont show something button</div>";
    
    } else {
    
    echo "<div>do nothing</div>";
    
    }
    
    ?>
    PHP:
     
    danx10, Jun 23, 2010 IP
  3. Chri

    Chri Active Member

    Messages:
    152
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #3
    Thanks For the Time spent for framing the code. Actually i tried using it, But Only the First If and Else Gets Exceuted and result shows. I.e Subdomain has no changes visible Why is that?
     
    Chri, Jun 23, 2010 IP
  4. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Try this
    
    
    <?php
    
    $url = str_replace("www.", "", $_SERVER['HTTP_HOST']);
    
    
    switch ($url)
    case 'domain.com':
    {
    echo "<div>dont show domain button</div>";
    break;
    case 'something.domain.com':
    echo "<div>dont show domain button</div>";
    break;
    default:
    echo "<div>dont do nothing</div>";
    break;
    }
    
    PHP:
    And say the Results :)
     
    roopajyothi, Jun 23, 2010 IP