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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
boom4x
Forum Newbie
Posts: 16
Joined: Mon Mar 08, 2004 2:10 pm
Location: New York, NY
Contact:

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

Post 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]
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
boom4x
Forum Newbie
Posts: 16
Joined: Mon Mar 08, 2004 2:10 pm
Location: New York, NY
Contact:

Post 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?
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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?
boom4x
Forum Newbie
Posts: 16
Joined: Mon Mar 08, 2004 2:10 pm
Location: New York, NY
Contact:

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

What does a var_dump($host); output ?
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
Post Reply