http_host with and without www

1.8k Views Asked by At

I have a message to appear in PHP if the domain going to is outside some existing ones already redirecting to the account.

For example, the command I am trying to run if the domain is www or non www is:

if( $_SERVER[ 'HTTP_HOST' ] != ('xxx.co.uk' && 'www.xxx.co.uk'))

However, this doesnt seem to work as if I go to xxxx.co.uk (which is another domain pointing to their account) the if content still do not show.

Am obviously missing something obvious with the above?

3

There are 3 best solutions below

1
On BEST ANSWER

Try this use " " instead of ' '

if( $_SERVER[ 'HTTP_HOST' ] != ("xxx.co.uk" && "www.xxx.co.uk"))

If the above one doesn't works try below one

if( ($_SERVER[ 'HTTP_HOST' ] != "xxx.co.uk") && ($_SERVER[ 'HTTP_HOST' ] != "www.xxx.co.uk"))
0
On

David Chen is right:

if ($_SERVER['HTTP_HOST'] != 'xxx.co.uk' || $_SERVER['HTTP_HOST'] == 'www.xxx.co.uk')

Check out the manual for more: http://php.net/manual/en/language.operators.comparison.php

0
On

Here you go. Why not use preg_match()?

if(!isset($_SERVER['HTTP_HOST']) || !preg_match('/^(www\.)?xxx(x)?\.co\.uk$/', $_SERVER['HTTP_HOST'])){
  header($_SERVER['SERVER_PROTOCOL'].' 400 Bad Request');
  die;
}