Re: set variables based on HTTP_HOST

From: Date: Mon, 08 Aug 2005 23:57:17 +0000
Subject: Re: set variables based on HTTP_HOST
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-220264@lists.php.net to get a copy of this message
Hi! On 8/8/05, Richard Davey <rich@launchcode.co.uk> wrote: > Why are you creating lots of arrays and then using in_array to check > them? Just seems a little pointless in this instance as it gives you > no real benefit - comparing a one element array against a variable is > just... well.. comparing a variable with a variable! So why not do > that? Perhaps a switch block would serve your needs better? I took your advice and put this up--any thoughts or advice would be appreciated. Is the switch setup below the sort of thing you were talking about? I altered it slightly overall to set a specific header file, instead of a graphic, which is more useful. <? // header generation script // define path to includes & header folder where // include files live $includepath = '/home/user/public_html/inc'; (( would be in a global include file, just here for clarity )) // see what host is invoked switch ($_SERVER['HTTP_HOST']) // check hostname { case 'domain.com': // define host $Header = '/inc/main.header.inc'; // define header file break; // next case 'www.domain.com': $Header = '/inc/main.header.inc'; break; case 'host1.domain.com': $Header = '/inc/host1.header.inc'; break; case 'host2.domain.com': $Header = '/inc/host2.header.inc'; break; case 'host3.domain.com': $Header = '/inc/host3.header.inc'; break; case 'host4.domain.com': $Header = '/inc/host4.header.inc'; break; case 'host5.domain.com': $Header = '/inc/host5.header.inc'; break; // etc., etc. default: $Header = '/inc/illegalhost.header.inc'; // define header } // call the include header file for that host if (file_exists("$includepath/$Header")) { // include valid? include stripslashes("$includepath/$Header"); // yup, include } else { echo "FAILURE MESSAGE OF SOME SORT"; // nope exit; } ?> (rest of page) I figure I can get a regexp in there somehow so I don't need two entries for the main domain.com and it's www c name, either... need to add that. I'm also sort of paranoid about unchecked includes in PHP and getting compromised--is doing a check like I am here for the include file's existence worthwhile or even useful to protect against possible problems? thanks, Joe

« previous php.general (#220264) next »