Share this question

Welcome to Teachnovice Q&A, where you can ask questions and receive answers from other members of the community.

This is a collaboratively edited question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Text Change via HTTP HOST

0 like 0 dislike
1,736 views
I want to echo a certain site name based upon the parked domain being used.

In the header I have placed

<?php
$st1 = ('Site Title 1');
$st2 = ('Site Title 2');
$regdef = ('Default Title');
?>

and in the div to echo the site title I have placed

if ($_SERVER['HTTP_HOST'] == ('site1.com')) {
echo $st1;
} elseif
($_SERVER['HTTP_HOST'] = ('site2.com')) {
echo $st2;
} else {
echo $regdef;
}

I'm not getting in parse errors but the "Site Title" for the defined variables are not showing up either... what am I missing. I am fairly new to PHP and this is my first attempt at writing my own code that I haven't modified from someone elses.
asked Dec 30, 2011 by anonymous  

2 Answers

0 like 0 dislike
 
Best answer

I see two problems here, but if you aren't getting anything to print then there is likely another issue that I cannot see. But first let's deal with the two that I can see. 

1: 
if($_SERVER['HTTP_HOST'] == ('site1.com'))

Odds are you want to get that the HTTP_HOST contains some text (for example, in the event that there is a subdomain prefixed on there). It would be perhaps better to instead search for the containing text like the following: 

if(strpos($_SERVER['HTTP_HOST'], 'site1.com') !== 0)

Here, we use the !== comparison operator instead of != because a valid return value for strpos ( [us.php.net...] ) is 0 when it finds what we are looking for at the beginning of the string. Here, though, 0 takes on the boolean value FALSE in PHP, so we are checking that it is not false, instead of not zero. 

[us.php.net...] 

2: 
($_SERVER['HTTP_HOST'] = ('site2.com')) {

Here, you are assigning the value of $_SERVER['HTTP_HOST'] to the value 'site2.com' because of the assign operator (=). Clearly just a typo, but this normally would be a comparison operator (e.g. ===). 

Try to make these fixes and see if you don't get different results. 

answered Dec 30, 2011 by anonymous  
selected Nov 6, 2012 by sarwana
0 like 0 dislike
A little bit more background, i'm still completely new to PHP coding from scratch. I have 15 parked domains that I am going to have to check the HTTP_HOST against.

I'm basically wanting to do this, if the HTTP_HOST equals a predefined variable, than the text will equal "this", else if it is another parked domain that I have not defined as a variable, use "a default text".

I suppose I could use something like this with key/value pairs, as I don't understand the strpos fully.

$lookup["site1.com"]="Site 1 Text";
$lookup["www.site1.com"="Site 1 Text";
$lookup["site2.com"]="Site 2 Text";
$lookup["www.site2.com"="Site 2 Text";
$sitetitle=$lookup[$_SERVER['HTTP_HOST']];

and then pull it into the div with this

echo $sitetitle;
answered Dec 30, 2011 by anonymous  
...