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
I want to echo a certain site name based upon the parked domain being used.

In the header I have placed

$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'] == ('')) {
echo $st1;
} elseif
($_SERVER['HTTP_HOST'] = ('')) {
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. 

if($_SERVER['HTTP_HOST'] == (''))

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'], '') !== 0)

Here, we use the !== comparison operator instead of != because a valid return value for strpos ( [] ) 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. 


($_SERVER['HTTP_HOST'] = ('')) {

Here, you are assigning the value of $_SERVER['HTTP_HOST'] to the value '' 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[""]="Site 1 Text";
$lookup[""="Site 1 Text";
$lookup[""]="Site 2 Text";
$lookup[""="Site 2 Text";

and then pull it into the div with this

echo $sitetitle;
answered Dec 30, 2011 by anonymous