PHP cross-platform server IP address user-defined function sharing, phpip user-defined function _ PHP Tutorial

Source: Internet
Author: User
PHP cross-platform access to server IP address user-defined function sharing, phpip user-defined function. PHP cross-platform access to server IP address user-defined function sharing, phpip user-defined function recently needs to improve a log mechanism to monitor the script execution status of machines from different servers, PHP cross-platform access to server IP address user-defined function sharing, phpip user-defined function

Recently, a log mechanism needs to be improved to monitor the script execution status of machines from different servers. a function is compatible with windows, Linux, and web and command line modes.

Write the following function to meet the above requirements:

The code is 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 method for windows Servers
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;

Parse needs to improve a log mechanism recently to monitor the script execution status of machines from different servers...

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.