Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting hostname or IP address of server in PHP

Tags:

url

php

I basically need to create a anchor that will navigate to a different port on the server. For instance, our server is hosted on domain.com:555, we need a link to navigate to domain.com:777. The catch is that it's not always domain.com. We can expect the DNS to malfunction, in which case we will use ip address to navigate like xx.xx.xx.xx:555 and xx.xx.xx.xx:777.

I need get the hostname of the server where the PHP script is running. I tried using SERVER_ADDR, but that for reason gives me the private IP of the server.

So how do I get the domain / ip part of the url?

like image 223
HyderA Avatar asked Dec 04 '22 11:12

HyderA


2 Answers

You want:

$_SERVER['HTTP_HOST']

From the $_SERVER predefined variable, which, for 'HTTP_HOST' contains:

Contents of the Host: header from the current request, if there is one.

You might also find:

$_SERVER['SERVER_NAME']

useful, and this Stack Overflow discussion of their relative merits instructive.

like image 97
Dominic Rodger Avatar answered Dec 21 '22 04:12

Dominic Rodger


Try $_SERVER['HTTP_HOST']; this will give you the host name / domain name

like image 40
SimonDowdles Avatar answered Dec 21 '22 04:12

SimonDowdles