PHP gets the native IP address PHP gets the remote IP address

Source: Internet
Author: User
Tags preg

PHP gets the native IP address PHP gets the full code for the remote IP address:

  1. //
  2. echo $_server[' REMOTE_ADDR '];
  3. Native IP Address
  4. function Get_local_ip () {
  5. $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/";
  6. Get the native IP real address of the operating system for WIN2000/XP, Win7
  7. EXEC ("ipconfig", $out, $stats);
  8. if (!emptyempty ($out)) {
  9. foreach ($out as $row) {
  10. if (Strstr ($row, "IP") && strstr ($row, ":") &&!strstr ($row, "IPv6")) {
  11. $tmpIp = Explode (":", $row);
  12. if (Preg_match ($preg, Trim ($tmpIp [1]))) {
  13. return Trim ($tmpIp [1]);
  14. }
  15. }
  16. }
  17. }
  18. Get the native IP real address of the Linux type for the operating system
  19. EXEC ("Ifconfig", $out, $stats);
  20. if (!emptyempty ($out)) {
  21. if (Isset ($out [1]) && strstr ($out [1], ' addr: ')) {
  22. $tmpArray = Explode (":", $out [1]);
  23. $tmpIp = Explode ("", $tmpArray [1]);
  24. if (Preg_match ($preg, Trim ($tmpIp [0]))) {
  25. return trim ($tmpIp [0]);
  26. }
  27. }
  28. }
  29. Return ' 127.0.0.1 ';
  30. }
Copy Code
  • 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.