PHP cross-platform Get server IP address Custom function sharing, PHPIP Custom Function _php Tutorial

Source: Internet
Author: User

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 ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.