0

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?

Palemo
  • 217
  • 1
  • 5
  • 19
  • I think you need to repeat `$_SERVER['HTTP_HOST']` in your if statement. `if($_SERVER['HTTP_HOST'] != 'xxx.co.uk' || $_SERVER['HTTP_HOST'] == 'www.xxx.co.uk')` I'm unsure what you're asking though, `if the domain is www, or not www`? – Dave Chen Jul 25 '14 at 04:23
  • I think you're going to find what you're looking for here: http://stackoverflow.com/questions/1459739/php-serverhttp-host-vs-serverserver-name-am-i-understanding-the-ma – StackSlave Jul 25 '14 at 04:47

3 Answers3

0

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"))
Kumaran
  • 3,460
  • 5
  • 32
  • 34
  • The first one would never work correctly — the `&&` operator does not work like that. –  Jul 25 '14 at 05:07
0

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

guillermoandrae
  • 600
  • 5
  • 10
0

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;
}
StackSlave
  • 10,613
  • 2
  • 18
  • 35