I am trying to find the best way to have a DIV be hidden if the url is something. For example I have 2 sites using the same template but only 1 of those sites I want to display something.

So site A is domain.com Site B is site.domain.com

I want it so if site.domain.com is where people are at then do not show DIV ID="hide". I also need it to have this work for not just that specific URL but for anything that comes after it so site.domain.com/aboutus.php, site.domain.com/contact.php etc....

I would like to do this with PHP or JS.

share|improve this question
PLEASE NOTE: You will need to do this with PHP if the DIV needs to be hidden for security purposes. If you are hiding the DIV for usability reasons, JS will suffice. – JSK NS Sep 19 '12 at 18:44
But these are two separate sites, running on separate databases, correct? The only common thing is the template? – relentless Sep 19 '12 at 18:45

4 Answers

up vote 2 down vote accepted

Just check HTTP_HOST:

<?php
    if($_SERVER['HTTP_HOST'] != 'site.domain.com'){
        echo '<div>contents</div>'; // display div, he is not on site.domain.com
    }
?>
share|improve this answer
PERFECT THank you! – user1683991 Sep 19 '12 at 19:11

You can probably do something like:

<php if($_SERVER['HTTP_HOST'] == 'example.com'): ?>
<div>This is example.com</div>
<?php else: ?>
<div>This is NOT example.com</div>
<?php endif; ?>

But I think you'd really be better off creating some sort of site1_settings.php that gets included on every page in one site, and then site2_settings.php that gets included in the second. For one thing, this will make it much easier to test your code locally.

share|improve this answer

I usually use strpos for this.

if(strpos($_SERVER["HTTP_HOST"], "site") !== FALSE)
{
  // The user is on site.domain.com
}
else
{
  // The user is on domain.com
}
share|improve this answer

Here would be my javascript implementation. Note: jQuery

if(window.location.indexOf("textinurl"))
{
    $('#DivId').hide();

}
share|improve this answer
Why use jQuery, when a simple document.getElementById('DivId').style.display = 'none'; would do just as well? – David Thomas Sep 19 '12 at 18:48
Either way works. jQuery is great for clean code and extensibility. If all OP ever needs to do with this code block is hide the div, you're correct. – JSK NS Sep 19 '12 at 18:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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