0

I am developing a site, I was wondering if I could use PHP in a generic way in order to show a div on the home page, but no on any other page. The code I have so far,

<?php
    if host == 'http://domain.com/'; echo {
        This should only be shown on the homepage http://domain.com but not on domain.com/directory or sub.domain.com/ !
    } else {
    };
?>
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65

2 Answers2

1
<?php
    $match_domains = array('google.com','www.google.com');
    if(in_array($_SERVER['SERVER_NAME'], $match_domains) {
        echo 'show on my domain!';
    }
?>

Using $_SERVER['SERVER_NAME'] we compare it to our desired domain. We use in_array to search $match_domains for the current domain. If it's in the array, we show our text...Anything else, we ignore.

<?php
    $domain = str_replace('www.','',$_SERVER['SERVER_NAME']); // Strip out www.
    $match_domains = array('google.com');
    if(in_array($domain, $match_domains) {
        echo 'show on my domain!';
    }
?>
Thomas
  • 1,401
  • 8
  • 12
  • thanks this works but how would I stop the content from showing in directory's – user3576563 Apr 26 '14 at 17:43
  • It's an absolute comparison, so it won't work on anything but the root domain. Even www., so you could do instead this (editing, one moment). – Thomas Apr 26 '14 at 17:44
  • yes i understand this but when i force www. on my server it forces it on my subdomains e.g www.business.domain.com and www.myaccount.domain.com – user3576563 Apr 26 '14 at 17:47
  • I'm sorry, I'm not understanding your question. Also if you wanted, you could strip out the www. so you're list is only main domains. – Thomas Apr 26 '14 at 17:48
  • Its ok i resolved the problem with my .htaccess I just forced the domain with non-www rewrite Thanks for all of your help – user3576563 Apr 26 '14 at 17:51
0

Since you want the home page why don't you check the file name

if ($_SERVER['SCRIPT_NAME'] == "/index.php") {
    echo "<div>Content</div>";
}
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65