11

I have the following piece of coding on a multi-domain 1 template setup:

<?php  $host = parse_url($domain, PHP_URL_HOST);
if($host == 'www.justdoors.co') {
echo "action goes here";
} ?>

For some reason it's not carrying out the action when I'm on the www.justdoors.co domain, what am I missing?

2
  • 2
    What is the value of $domain?
    – user142162
    Commented Jun 29, 2011 at 14:10
  • 2
    What is the value of $host when you are on that domain?
    – Jon
    Commented Jun 29, 2011 at 14:11

3 Answers 3

16

Use $_SERVER['SERVER_NAME'] or $_SERVER['HTTP_HOST'] variable

 $host = $_SERVER['HTTP_HOST']; 
 if($host == "www.justdoors.co" or $host == "justdoors.co") {
     //do anything you want to do 
 }
3
  • $_SERVER['HTTP_HOST'] === $_SERVER['SERVER_NAME'] is not always true. HTTP_HOST will have a '.' at the end if you make the host absolute: http://example.org./ (http://example.org/ [without the dot] is relative).
    – Saxoier
    Commented Jun 29, 2011 at 14:43
  • You should never ever trust user input.
    – Saxoier
    Commented Jun 29, 2011 at 14:57
  • Sure, in case it's security issue, it should be SERVER_NAME. In case it's template selection, ONLY USER losts something
    – genesis
    Commented Jun 29, 2011 at 14:59
7

I have used this successfully on my server before.

<?php if ( $_SERVER['SERVER_NAME'] == 'www.domain.com' ) { echo ''; } ?>
1

I have tried this and it's working perfectly ..

<?php
function conn()
{
    $ora_con=0;
    //development connection building
    if ( $_SERVER['SERVER_NAME'] == 'xyz.com' ) 
    {$username='scott';
     $password = 'tiger';
     $ora_conn_string = 'conn_name:port_no/db_name';
     //echo "<br>development connection building";
      $ora_con = oci_connect($username,$password,$ora_conn_string);
     //echo '<br>dev connection build<br>';
    }
    //Production connection building
    elseif ( $_SERVER['SERVER_NAME'] == 'abc.net' ) 
        {$username='prod_scott';
         $password = 'prod_tiger';
         $ora_conn_string = 'conn_name:port_no/db_name';
         //echo "<br>Production connection building";
         $ora_con = oci_connect($username,$password,$ora_conn_string);
         //echo '<br>Prod connection build<br>';
        }

    return $ora_con;
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.