>  기사  >  백엔드 개발  >  php获取本机ip地址 php获取远程IP地址

php获取本机ip地址 php获取远程IP地址

WBOY
WBOY원래의
2016-07-25 09:11:561115검색

php获取本机ip地址 php获取远程IP地址 完整代码:

  1. //
  2. echo $_SERVER['REMOTE_ADDR'];
  3. //本机IP地址
  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. //获取操作系统为win2000/xp、win7的本机IP真实地址
  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. //获取操作系统为linux类型的本机IP真实地址
  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. }
复制代码


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.