Is there a better way to check domain than SERVER_NAME?

matt3

Member
Jun 15, 2009
660
9
18
Is there a better way to check for if domain then echo? Does this need to be closed out with a generic else, and at what point does this become resource intensive across a bunch of domains?

Thanks for your help!

Code:
<?php
$host = $_SERVER['SERVER_NAME']; 
 if($host == "www.site.com" or $host == "site.com")
    { echo '<a href="http://site1.com">anchor 1</a> | <a href="http://site2.com">anchor 1</a> | <a href="http://site3.com">anchor 1</a>'; } 
    
 if($host == "www.site.com" or $host == "site.com")
    { echo '<a href="http://site1.com">anchor 2</a> | <a  href="http://site2.com">anchor 2</a> | <a  href="http://site3.com">anchor 2</a>'; } 

 if($host == "www.site.com" or $host == "site.com")
     { echo '<a href="http://site1.com">anchor 3</a> | <a  href="http://site2.com">anchor 3</a> | <a  href="http://site3.com">anchor 3</a>'; } 
    
?>
 


If I understand you correctly...

Code:
$anchors = array(
'site1.com' => 'anchor 1',
'site2.com' => 'anchor 2',
'site3.com' => 'anchor 3'
);

foreach($anchors as $site=>$anchor) {
  if($_SERVER['SERVER_NAME'] == $site) {
      echo '<a href="site.com">'.$anchor.'</a>';
  }
}
Just a shitty example, but you get the idea.
 
Adding to Houdas... (not tested)

Code:
$anchors = array(
    'site1.com' => 'anchor 1',
    'site2.com' => 'anchor 2',
    'site3.com' => 'anchor 3'
);

foreach($anchors as $site => $anchor)
{
    $host = str_replace('www.', '', $_SERVER['HTTP_HOST']);
    if($host == $site)
    {
        echo '<a href="site.com">'.$anchor.'</a>';
    }
}
 
It's annoying me to see a total lack of error checking in this thread.

Anyway, I'm guessing the goal is to have a link circle jerk going on like so:

Code:
$anchors = array(
    'site1.com' => array('fqd' => 'www.site1.com/rackcity' 'anchor' => 'rack city bitch',
    'site2.com' => array('fqd' => 'www.site2.com/lolmoneylol' 'anchor' => 'lol money lol',
    'site3.com' => array('fqd' => 'www.site3.com/badcodersftw' 'anchor' => 'bade coders ftw',
);

if (isset($_SERVER['HTTP_HOST']))
{
    $host = str_replace('www.', '', strtolower($_SERVER['HTTP_HOST']));
    foreach($anchors as $site => $arr)
    {
        if($host != $site)
            echo "<a href=\"{$arr['fqd']}\">{$arr['anchor']}</a>";
    }
}
 
Whoa... I have not one piece of coder DNA in my body, simply slapping shit together to do what I need.

That said, my example is currently working and easier to understand, for me, I just don't know if it's wrong or will cause errors. Yes Insomniac, the idea is to display different anchor text for the same links depending on which domain they're displayed on. (central file to edit links rather than logging into each site in my network "/home/footerlinks.php" include files).

Additionally, what is the correct way to close out my example with an else statement that features a generic link list if a domain is not specified? (say site47.com doesn't have links specified, I want it to default to the standard link pool)

Thanks for your help guys, much appreciated!
 
To avoid random server errors and implement better programming practice, use Insomniac's code. I just made a couple tweaks to include your original request of printing out a default link list.

Code:
$anchors = array('site1.com' => array('fqd' => 'www.site1.com/rackcity', 'anchor' => 'rack city bitch'),'site2.com' => array('fqd' => 'www.site2.com/lolmoneylol', 'anchor' => 'lol money lol'),'site3.com' => array('fqd' => 'www.site3.com/badcodersftw', 'anchor' => 'bade coders ftw') );
$defaultSites = array('site4.com' => 'default anchor 1','site5.com' => 'default anchor 2','site6.com' => 'default anchor 3');

$match = false;

if (isset($_SERVER['HTTP_HOST'])) {
    $host = str_replace('www.', '', strtolower($_SERVER['HTTP_HOST']));     
    foreach($anchors as $site => $arr)     
    {         
        if($host == $site) 
        {
            echo "<a href=\"{$arr['fqd']}\">{$arr['anchor']}</a>";
            $match = true;
        }
    } 
}

if(!$match) 
{
    foreach($defaultSites as $site => $anchor)        
        echo "<a href=\"{$site}\">{$anchor}</a>"; 
}
 
Felt obligated to continue the circle jerk.

Code:
$anchors = array(
    'site1.com' => array('fqd' => 'www.site1.com/rackcity' 'anchor' => 'rack city bitch',
    'site2.com' => array('fqd' => 'www.site2.com/lolmoneylol' 'anchor' => 'lol money lol',
    'site3.com' => array('fqd' => 'www.site3.com/badcodersftw' 'anchor' => 'bade coders ftw'
);

if (isset($_SERVER['REQUEST_URI']))
{
    $host = str_replace('www.','',$_SERVER['HTTP_HOST']);

    if(count($anchors) > 0){
        foreach($anchors as $site => $arr)
        {
            if($host != $site)
                echo "<a href=\"{$arr['fqd']}\">{$arr['anchor']}</a>";
        }
    }
}
 
Passed the 10 min mark on the previous post.. Left out the strtolower by accident but that should be there. However I like this version.

Code:
$anchors = array('site1.com' => array('fqd' => 'www.site1.com/rackcity', 'anchor' => 'rack city bitch'),'site2.com' => array('fqd' => 'www.site2.com/lolmoneylol', 'anchor' => 'lol money lol'),'site3.com' => array('fqd' => 'www.site3.com/badcodersftw', 'anchor' => 'bade coders ftw') );
$defaultSites = array('site4.com' => 'default anchor 1','site5.com' => 'default anchor 2','site6.com' => 'default anchor 3');

$match = false;

if (isset($_SERVER['HTTP_HOST']) AND count($anchors) > 0) {
    $host = str_replace('www.', '', strtolower($_SERVER['HTTP_HOST']));     
    foreach($anchors as $site => $arr)     
    {         
        if($host == $site) 
        {
            echo "<a href=\"{$arr['fqd']}\">{$arr['anchor']}</a>";
            $match = true;
        }
    } 
}

if(!$match AND count($defaultSites) > 0) 
{
    foreach($defaultSites as $site => $anchor)        
        echo "<a href=\"{$site}\">{$anchor}</a>"; 
}

I know where defining the variable up top so it's safe to assume the arrays will be there but it's always good practice to check!
 
Developer circle jerk! Yee-haw! Here's mine!

Code:
$anchors = array(
     'site1.com' => array('www.site1.com/rackcity' => 'my anc text'), 
     'site2.com' => array('www.site2.com/rackcity' => 'my anc text 2'), 
     'site3.com' => array('www.site3.com/rackcity' => 'my anc text 3'), 
     'default.com' => array(
          'www.default.com/rackcity' => 'default anc text', 
          'www.default2.com/rackcity' => 'default anc text 2'
     )
);

// Set variables
$host = isset($_SERVER['SERVER_NAME']) ? preg_replace("/^www\./", "", strtolower($_SERVER['SERVER_NAME'])) : 'default.com';
$links = isset($anchors[$host]) ? $anchors[$host] : $anchors['default.com'];

// Display links
foreach ($links as $url => $text) { 
    echo "<a href=\"$url\">$text</a>\n";
}
You'd be better of shoving your links into a mySQL database, so you can just update them as needed via phpMyAdmin though. That, and so PHP doesn't have to load your entire list of links for every page load on every site.
 
You'd be better of shoving your links into a mySQL database, so you can just update them as needed via phpMyAdmin though. That, and so PHP doesn't have to load your entire list of links for every page load on every site.

No it'll just make a mysql connection instead.... ?
 
Depends how many sites he has, and whether or not he wants to plan for future expansion now. And more than likely, his site is already connecting to a database anyway. I don't think there's many sites out there that don't have a database behind them these days.
 
Depends how many sites he has, and whether or not he wants to plan for future expansion now. And more than likely, his site is already connecting to a database anyway. I don't think there's many sites out there that don't have a database behind them these days.

I'm not saying anything about not using database query, just that it's wrong to suggest it would use less resources than including a simple array within the script that's being called.
 
I'm not saying anything about not using database query, just that it's wrong to suggest it would use less resources than including a simple array within the script that's being called.

Again, depends on the # of sites. If he has say 1200 going, then a database would be less resource intensive. If he has 6 sites, then yeah, an array is fine.
 
Please tell me how it would be less resource intensive?
 
What? Would you like me to break it down into bytes & packet sizes for you?

I don't know where the cutoff point is, but guaranteed there's a point where connecting to a mySQL database & running 1 query becomes less intensive then loading an array of X size.