PHP cross-platform custom function sharing for obtaining server IP address, phpip custom function
Recently, we need to improve a log mechanism to monitor the script execution status of machines from different servers. We have written a function specifically for Windows, Linux, web and command line modes to make it compatible.
I wrote the following function to achieve the above requirements:
Copy code The code is as follows:
function getServerAddr() {
//Run web app
If (isset($_SERVER["SERVER_ADDR"])) {
return $_SERVER["SERVER_ADDR"];
} else { // Running CLI
If (stristr(PHP_OS, 'WIN')) {
to
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|((1d{2})|([1-9]?d))).){3} (?:25[0-5]|2[0-4]d|((1d{2})|([1-9]?d))))/', $ip , $match);
Return $match[1];
$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 sharing of server IP address custom function sharing, phpip custom function needs to improve a log mechanism and monitor in the near future Script execution status of machines from different servers, especially...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn