Home  >  Article  >  Backend Development  >  php gets local ip (remote IP address)

php gets local ip (remote IP address)

WBOY
WBOYOriginal
2016-07-25 09:12:201937browse

Example, php gets user IP address.

  1. // 111111111111
  2. echo $_SERVER['REMOTE_ADDR'];
  3. // 222222222222
  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. } bbs.it-home.org
  17. }
  18. //Get the real IP address of the machine whose operating system is Linux type
  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