Tag Archives: ip

PHP: Get local ip adresses using ifconfig (linux only)

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , , , , . .

Hi,

I wrote little code to get local machine ip address:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        function get_machine_ips()      {
                $ips = array();
                exec("/sbin/ifconfig", $catch);
                foreach($catch as $line){
                if (eregi('inet addr:', $line)) {
                        $line = str_replace(" ", ":", $line);
                        $line = explode(":", $line);
                        $line = array_filter($line);
                        foreach ($line as $v)   {
                                if (ip2long($v))        {
                                        $ips[] = $v;
                                }
                        }
                }
                }
                return $ips;
        }

have fun
Garry

Share