-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?

Cuaox
  • 69
  • 5
  • well yeah, a server to itself is 127.0.0.1 ... – treyBake May 31 '19 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 – Cuaox May 31 '19 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 – Cuaox May 31 '19 at 15:50
  • you don't need www to access a website - just swap the url for yours? – treyBake May 31 '19 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? – Cuaox May 31 '19 at 15:54
  • The first "dupe" just returns 127.0.1.1, the second "dupe" is the address of the connecting host, not the server. I think you are trying to forcefully discredit this question. I know it's simple, but I can also search stackoverflow you know. – Cuaox May 31 '19 at 16:01

2 Answers2

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(); 
?>
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

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

exec ("hostname -I", $ip);
echo $ip[0];
Cuaox
  • 69
  • 5