Forum Moderators: coopster

Message Too Old, No Replies

if HTTP HOST variable issues

         

Readie

9:49 pm on Dec 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, I'm fairly new to PhP, and am having trouble with a particular script I am trying to run. I have two domains, and am trying to make small differences between the two using an if statement like so:

if (($_SERVER['HTTP_HOST'] == "example.co.uk") OR ($_SERVER['HTTP_HOST'] == "www.example.co.uk"))
{
$titlebar='¦ Winterwind PvP';
$title='<map name="homepage"><area shape="rect" href="http://www.example.co.uk/index.php" coords="39,40,417,101" /></map><img src="sitemain/title.png" border="0" usemap="#homepage" />';
$nav='example.co.uk">Example';
}

else if (($_SERVER['HTTP_HOST'] == "example.co.uk") OR ($_SERVER['HTTP_HOST'] == "www.example.co.uk"))
{
$titlebar='¦ Example PvP';
$title='<map name="homepage"><area shape="rect" href="http://www.example.co.uk/index.php" coords="39,40,417,101" /></map><img src="sitemain/rrtitle.png" border="0" usemap="#homepage" />';
$nav='example.co.uk">Example&nbsp;site';
}

This is located outside of any function

I am then trying to pull the variables inside the same .php file, inside an echo command, like so:

echo ' ...snip... <td colspan="3" height="200" width="800">' . $title . '</td> ...snip... ';

With similar for the other variables.

However, when trying to run this script, I'm just left with a blank space where I am trying to echo the variable; other than that, the page is working fine.

Any assistance in this matter would be very greatly appreciated :)

[edited by: eelixduppy at 10:44 pm (utc) on Dec. 13, 2009]

[edited by: dreamcatcher at 12:35 am (utc) on Dec. 14, 2009]
[edit reason] exemplified [/edit]

rocknbil

2:27 am on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcoem aboard readie, echo $_SERVER['HttP_HoST'] somewhere to the screen so you're sure it's the value you expect.

Also, I did that case thing for a reason. == expects exact values, a preg_match can be made case insensitive.

dreamcatcher

2:32 am on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



also, set the error reporting to E_ALL at the top of your php file that calls the variables.

error_reporting(E_ALL);

That may give you some clues as to the problem.

dc

Readie

3:50 pm on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you both for replying so fast.

To Rocknbil: I know that the if statement itself is working, a simple echo command inside works fine and it does differentiate between the two URLs. Thanks for the advice about making it case-insensitive though. Cheers for the welcome too :)

To Dreamcatcher: Error reporting confirmed my suspicions that the variable declaration wasn't working, stating that there were several undefined variables. I can't see anything wrong with my syntax though :/

Readie

4:21 pm on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahh, fixed it. I believe this was quite a newbie mistake of me, but i needed to have function f2($wwrr, $title, $titlebar, $nav) { in the format.php and f2($wwrr, $title, $titlebar, $nav) in the index.php

jdMorgan

4:34 pm on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also be aware that HTTP_HOST can be in FQDN format -- for example, "example.com." with a trailing period, and can also contain a port number appended to the hostname, as in "example.com:80".

Both of these could be present at the same time, so you'd see "example.com.:80".

So unless you've got some code elsewhere (e.g. in a .htaccess file) that will redirect requests for non-canonical hostnames back to the canonical hostnames, your exact-match test will fail in any of those three cases.

For SEO reasons, I strongly recommend that you implement some code to force domain canonicalization and 'fix' all of these www/non-www/FQDN/port issues before invoking PHP. But if you don't do that, then consider using a regular-expressions pattern match to test HTTP_HOST, using something like
^(www\.)?example\.com\.?(:[0-9]+)?$

This one pattern in a preg_match will handle all those www/non-www, FQDN, and port variations of the hostname.

Jim

Readie

5:33 pm on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jdMorgan: I have [example.co.uk][example2.co.uk][www.example.co.uk][www.example2.co.uk] set as valid hosts on the server. If you try and do something like you wrote you'll just get a 404 error.

On a general note: I've put the if statements inside the actual functions on my format.php now so I don't need to have the variables as a function(argument) - saves me having to modify the entire site if I decide to make adjustments.

jdMorgan

7:16 pm on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> jdMorgan: I have [example.co.uk][example2.co.uk][www.example.co.uk][www.example2.co.uk] set as valid hosts on the server. If you try and do something like you wrote you'll just get a 404 error.

I hope you actually tested this. If you did get a 404 error then that's technically not a good thing, because requests for FQDN hostnames and with appended port numbers are quite valid. Instead, these requests should be 301-redirected to the canonical hostname.

Note that in your DNS settings, each domain specified in an "A record" must end with a period, because a fully-qualified domain name is *required* in DNS zone files. I'm mentioning that in case you have any doubts about such hostnames being valid.

I've described best practices here. It's your option whether to follow them or not.

Jim

Readie

8:40 pm on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jdMorgan: Thank you for the advice :). As I said, I'm new to PhP and also hosting in general: before now I've always used free hosting and stuck to HTML/CSS (with the occasional JavaScript) so please forgive the statement given in ignorance that sounded like an argument.

I'm not 100% certain of the syntax for the preg_match, but I also don't want to be flat out told it as I'll never actually learn it that way. I shall have a few attempts when I'm back at the PC that has the files on.