How to get the my system's IP address

前端 未结 4 548
野性不改
野性不改 2020-12-22 11:50

How do I get my system\'s IP address?

I am using $ip_address=$_SERVER[REMOTE_ADDR];, but it returns my localhost IP address instead of my system\'s IP

4条回答
  •  暖寄归人
    2020-12-22 12:02

    A late reply but I hope it still might be able to help someone. I needed system ip for a project so I quickly scrapped up a custom function using regular expressions. It works on ubuntu linux (and technically should work on any distro/version of linux).

    function getLocalIp(){
        $output = shell_exec('/sbin/ifconfig');        
        preg_match("/inet?[[:space:]]?addr:([0-9.]+)/", $output, $matches);
        if(isset($matches[1]))
            $ip = $matches[1];
        else
            $ip = 0;
        return $ip;
    }
    

    It requires:

    • making a system call using shell_exec.
    • using regular expressions to find the value of internal ip address.

    If you happen to create something similar for windows or improve the functionality of this function, please share your code with the rest of us.

提交回复
热议问题