Php method and instance code to get native real IP address

Source: Internet
Author: User
Tags preg
This article mainly for you in detail the PHP to obtain the real IP address instance code, with a certain reference value, interested in small partners can refer to

The main is to obtain the operating system for WIN2000/XP, Win7 native IP real address, and obtain the operating system for the Linux type of native IP real address, specific content as follows

function Getlocalip () {$preg = "/\a (([[0-9]?[ 0-9]) | (1[0-9]{2}) | (2[0-4][0-9]) | (25[0-5])) \.) {3} ([0-9]? [0-9]) | (1[0-9]{2}) | (2[0-4][0-9]) | (25[0-5])) \z/";//Get the native IP address of the operating system WIN2000/XP, Win7 exec (" ipconfig ", $out, $stats); if (!empty ($out)) {  foreach ($out as $row) {   if (strstr ($row, "IP") && strstr ($row, ":") &&!strst R ($row, "IPv6")) {    $tmpIp = explode (":", $row);    if (Preg_match ($preg, Trim ($tmpIp [1])) {     return trim ($tmpIp [1]);}}}  Get the native IP real address of the Linux type for the OS exec ("Ifconfig", $out, $stats); if (!empty ($out)) {  if (isset ($out [1]) && strstr ($out [1], ' addr: ')} {   $tmpArray = explode (":", $out [1]);   $tmpIp = Explode ("", $tmpArray [1]);   if (Preg_match ($preg, Trim ($tmpIp [0])) {    return trim ($tmpIp [0]),}   }  } return ' 127.0.0.1 ';}

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.