Page 1 of 1

Need help on $_SERVER['HTTP_HOST'] and php code

Posted: Wed May 12, 2004 9:21 pm
by boom4x
Hi,

I am trying to use the $_SERVER['HTTP_HOST'] , to show html at the top of the page. (for every "parked" name on my server points only to one website but when someone goes on the site and types in for ex. yahoo.com, or excite.com thats what shows up at the top, ONLY it has to be in html so i can change the look of it)

OK here is the code:

Code: Select all

<?

/// Start Settings ////
$yahoo='<b><font style="FONT-SIZE: 24pt" face="Verdana"><font color="#000080">
Yahoo</font><i><font color="#ff0000">.com</font></i></font></b>';

$excite='<b><font style="FONT-SIZE: 24pt" face="Verdana"><font color="#000080">
Excite</font><i><font color="#ff0000">.com</font></i></font></b>';

/// End Settings ////

/// List Functions
if($_SERVER['HTTP_HOST'] == "yahoo.com" OR "www.yahoo.com" OR "tv.yahoo.com" OR "mail.yahoo.com")
{
print($yahoo);
}

else if($_SERVER['HTTP_HOST'] == "excite.com" OR "www.excite.com" OR "tv.excite.com" OR "weather.excite.com")
{
print($excite);
}

/// If nothing is privided we will display the below message
else
{
echo ( "Wrong Parameters" );
}
?>
Thats what i got so far.. Also, instead of writing out all sub-domains, can I use for ex. "*.yahoo.com"?? (will it still work if someone just types in "yahoo.com"?)

Thank you in advance.

[/php_man]

Posted: Wed May 12, 2004 9:35 pm
by launchcode
This might save you some time, a modification of a script I wrote that does something similar:

Code: Select all

<?
	//	Just add more of these, changing the ['name'] part to match the $domains array below
	$html['yahoo'] = '<b><font style="FONT-SIZE: 24pt" face="Verdana"><font color="#000080">Yahoo</font><i><font color="#ff0000">.com</font></i></font></b>';

	$html['excite'] = '<b><font style="FONT-SIZE: 24pt" face="Verdana"><font color="#000080">Excite</font><i><font color="#ff0000">.com</font></i></font></b>'; 

//	The following 2 lines were just used to test the code, hence why they are commented out
//	$url = "http://mail.excite.com";
//	$host = parse_url($url);

	$host = parse_url($_SERVER['HTTP_HOST']);

	//	Add new ones like below, the first part ('yahoo') should match the HTML name above,
	//	the part after => should be the domain name you want to search against
	$domains = array('yahoo' => 'yahoo.com', 'excite' => 'excite.com');

	foreach ($domains as $site => $domain)
	{
		if (strpos($host['host'], $domain))
		{
			echo $html[$site];
		}
	}
?>
Reply if you need any of it explaining but I added comments for you.

Posted: Thu May 13, 2004 2:07 pm
by boom4x
Hi again, i just tried this new code.. it doesn't seem to work...

can you check it out and see if it work alright?

Posted: Thu May 13, 2004 5:46 pm
by launchcode
It works fine - check your end and post an error?

Bear in mind that excite.com, yahoo.com etc are EXAMPLE domains - they will _never_ work for you in real life without you changing them to something more meaningful, because the HTTP_HOST is the name of the server your script is running on - so unless you're coding this for the yahoo.com servers, it'll never equal yahoo.com.

Maybe what you really meant was to check the referer?

Posted: Fri May 14, 2004 12:24 am
by boom4x
OK we'll the script works only nothing comes up... and YES ofcourse, i have changed it the the proper values ;-)
When i checked for errors, this is what came up

Notice: Undefined index: host in /home/thecolla/public_html/18.php on line 20

Notice: Undefined index: host in /home/thecolla/public_html/18.php on line 20

I think everything is ok in the script except for the last

echo command.. not sure about it though.

Posted: Fri May 14, 2004 1:52 am
by markl999
What does a var_dump($host); output ?

Posted: Fri May 14, 2004 6:07 am
by launchcode
Post *your* modified version of the script ;)
Because I know the one I put up there works okay.

An undefined host error would mean only one thing - there is nothing in the $_SERVER['HTTP_HOST'] value so parse_url has failed.