PHP cross-platform Get server IP address Custom function sharing, PHPIP custom function
There is a need to refine a log mechanism to monitor the script execution status of machines from different servers, specifically for Windows and Linux and for Web and command-line mode to write a function to be compatible.
Write the following function to achieve the above requirements:
Copy the Code code as follows:
function Getserveraddr () {
Run the web App
if (Isset ($_server["server_addr")) {
return $_server["Server_addr"];
} else {//Running CLI
if (Stristr (Php_os, ' WIN ')) {
A hacky approach to Windows Server execution
EXEC ("Ipconfig/all", $catch);
foreach ($catch as $line) {
$new _catch[] = iconv ("GBK", "Utf-8", $line). "\ n";
}
foreach ($new _catch as $new _line) {
if (Preg_match ('/ipv4 address/', $new _line)) {//Chinese system
List ($t, $ip) = Explode (': ', $new _line);
$ip = Trim ($IP);
Preg_match ('/(?:(? : 25[0-5]|2[0-4]\d| ((1\d{2}) | ([1-9]?\d))) \.) {3} (?: 25[0-5]|2[0-4]\d| ((1\d{2}) | ([1-9]?\d)))) /', $IP, $match);
return $match [1];
}
}
} else {
$ifconfig = Shell_exec ('/sbin/ifconfig eth0′);
Preg_match ('/addr: ([\d\.] +)/', $ifconfig, $match);
return $match [1];
}
}
}
$ip = Getserveraddr ();
Print $ip;
http://www.bkjia.com/PHPjc/934934.html www.bkjia.com true http://www.bkjia.com/PHPjc/934934.html techarticle PHP Cross-platform to get the server IP address custom function sharing, PHPIP Custom function recently need to improve a log mechanism to monitor the script execution status of machines from different servers, special needles ...