Heim  >  Artikel  >  Backend-Entwicklung  >  php获取本机ip地址 php获取远程IP地址

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

WBOY
WBOYOriginal
2016-07-25 09:11:561113Durchsuche

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. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn