How do I find my server's IP address in PHP(CLI)

前端 未结 6 1035
梦谈多话
梦谈多话 2020-12-13 09:12

Aside from the obvious (localhost, 127.0.0.1) does PHP (command line interface!) have a mechanism for discovering the IP of the computer the script is running on?

6条回答
  •  情歌与酒
    2020-12-13 09:50

    If you are working with PHP < 5.3, this may help (on *NIX based systems atleast):

     mscharley@S04:~$ cat test.php
    #!/usr/bin/env php
    
      string(13) "72.67.113.141"
      [1]=>
      string(27) "fe80::21c:c0ff:fe4a:d09d/64"
      [2]=>
      string(13) "72.67.113.140"
      [3]=>
      string(9) "127.0.0.1"
      [4]=>
      string(7) "::1/128"
    }
     mscharley@S04:~$
    

    Or, if you don't anticipate doing it often, then perhaps this would work (just don't abuse it):

    $ip = file_get_contents('http://whatismyip.org/');
    

提交回复
热议问题