-1

I need to display the address of the server by which it can be identified in LAN.
I tried using

echo $_SERVER["SERVER_ADDR"]; 

but this just returns 127.0.0.1 instead of 192.168.xx.xx.
How do i get the second one without the access to WWW?

6
  • well yeah, a server to itself is 127.0.0.1 ... Commented May 31, 2019 at 15:47
  • @treyBake I know it is, but I need a way to print it's LAN address. Im not asking why SERVER_ADDR isn't working, just stating that it doesn't Commented May 31, 2019 at 15:49
  • also, @treyBake to refer to the link you have posted, this is not a solution since my setup doesn't have access to www Commented May 31, 2019 at 15:50
  • you don't need www to access a website - just swap the url for yours? Commented May 31, 2019 at 15:52
  • I don't think you generally get what I'm trying to ask. I want the LAN address of my server. Not the external IP. You have posted a question which asks about external address and uses external sites as a solution. Why would it be a dupilcate? Commented May 31, 2019 at 15:54

2 Answers 2

1

Hope this will help,

function getLanIP(){ exec("ipconfig /all", $output); foreach($output as $line){ if (preg_match("/(.*)IPv4 Address(.*)/", $line)){ $ip = $line; $ip = str_replace("IPv4 Address. . . . . . . . . . . :","",$ip); $ip = str_replace("(Preferred)","",$ip); } } return $ip; } 

OR something like,

$ip = getLanIP(); echo $ip; <?php function getLocalIp(){ return gethostbyname(trim(`hostname`)); } echo getLocalIp(); ?> 
Sign up to request clarification or add additional context in comments.

2 Comments

code only answers ... ;)
Your answer pointed me to an even simpler approach, just use "hostname -I" (must include capital I), it outputs exactly what i need.
0

Solution was simpler than I thought it would be.
Just use:

exec ("hostname -I", $ip); echo $ip[0]; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.