Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_SERVER['HTTP_HOST'] contains port number too =/

I don't know maybe it's a bug.

I have 2 virutalhosts on my server.

virtualhost1.com virtualhost2.com

if i open virtualhost1.com with port 80 (virtualhost1.com:80)

$_SERVER['HTTP_HOST']='virtualhost1.com';

but if i open virtualhost2.com:80

$_SERVER['HTTP_HOST']='virtualhost2.com:80'; // NOTE: with port number

Can I know why?

like image 670
dynamic Avatar asked Dec 21 '10 23:12

dynamic


2 Answers

The value of $_SERVER['HTTP_HOST'] is taken directly from the Host: HTTP request header. It appears the requesting client is filling it in that way.

I suggest using $_SERVER['SERVER_NAME'] instead as its value will be set from your virtual host config. However, as Flimm notes below, even the reliability of SERVER_NAME can still be dependent on server config (check out this answer for more info on that).

like image 80
webbiedave Avatar answered Sep 30 '22 00:09

webbiedave


Following function always return real host (user typed host) without port and it's almost reliable:

function getRealHost(){
   list($realHost,)=explode(':',$_SERVER['HTTP_HOST']);
   return $realHost;
}
like image 34
MSS Avatar answered Sep 30 '22 00:09

MSS