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

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , , , , . Bookmark the Permalink. Post a Comment. Leave a Trackback URL.

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

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*