在命令行脚本中使用时使用PHP $ _SERVER ['SERVER_ADDR']变量

It seems as if I cannot use the $_SERVER vars in a command line script in PHP as the return seems to be blank? If this is the case then does anyone have a suggestion for getting the IP of the server I am running the script on?

Thanks in advance.

SERVER_ADDR is defined by the webserver as CGI environment variable, and depends upon which interface it listens to. A server might have multiple network addresses.

You can fetch it from either ip addr or ifconfig, preferrably looking for the eth0 interface (most common).

preg_match("/inet (addr:)?(?!127)(\d+(\.\d+){3})/", `ip addr`, $m);