PHP Get native Real IP address instance code _php tips

Source: Internet
Author: User
Tags explode preg

This example for you to share the PHP to obtain the real IP address instance code, for your reference.

The main is to obtain the operating system for WIN2000/XP, Win7 native IP real address, and get the operating system for Linux type of the native IP real address, the specific contents are 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 operating system for WIN2000/XP, Win7 native IP real address
 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 operating system for Linux type native IP real address
 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 ';
}

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

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.