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

I don’t know, maybe this is a mistake.

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 

May I find out why?

+8
php superglobals virtual-hosts port-number
source share
2 answers

The value of $_SERVER['HTTP_HOST'] is taken directly from the header of the HTTP Host: request. The requesting client seems to fill it in this way.

I suggest using $_SERVER['SERVER_NAME'] instead, as its value will be set from your virtual host configuration. However, as Flimm notes, even the reliability of SERVER_NAME may still depend on the server configuration (check out this answer for more information about this).

+16
source

The following function always returns a real host (user typed host) without a port, and it is almost reliable:

 function getRealHost(){ list($realHost,)=explode(':',$_SERVER['HTTP_HOST']); return $realHost; } 
+1
source

Source: https://habr.com/ru/post/650363/


All Articles