Home  >  Article  >  Backend Development  >  php gets the local ip address php gets the remote ip address

php gets the local ip address php gets the remote ip address

WBOY
WBOYOriginal
2016-07-25 09:11:561175browse

php gets the local IP address php gets the remote IP address Complete code:

  1. //
  2. echo $_SERVER['REMOTE_ADDR'];
  3. //Local 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 real IP address of the machine whose operating system is win2000/xp or 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 operating system of Linux type The real IP address of the machine
  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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn