Home  >  Article  >  Backend Development  >  PHP get IP, MAC address function

PHP get IP, MAC address function

黄舟
黄舟Original
2016-12-21 10:14:561964browse

参数$AiType取值:1:IP,2:MAC,3:IP+MAC
返回 return string:地址串

function GetClientAddr($AiType=0) 

  $sMac = ""; 
  $ip= getenv("REMOTE_ADDR");  
  $ip1 = getenv("HTTP_X_FORWARDED_FOR");  
  $ip2 = getenv("HTTP_CLIENT_IP");  
  ($ip1) ? $ip = $ip1 : null;  
  ($ip2) ? $ip = $ip2 : null;  
  if ($AiType > 1) 
  { 
     $sCmd = "nbtstat -a " . $ip; 
     $sString = shell_exec($sCmd); 
     $aTemp = split("=",$sString); 
     $sMac = trim($aTemp[count($aTemp)-1]); 
  } 
  switch($AiType) 
  { 
    case 0: 
      $sResult = ""; 
      break; 
    case 1: 
      $sResult = $ip; 
      break; 
    case 2: 
      $sResult = $sMac; 
      break; 
    case 3: 
      $sResult = $ip . " " . $sMac; 
      break; 
    default: 
      $sResult = ""; 
      break; 
   } 
    return $sResult; 

?>    

参数$AiType取值:1:IP,2:MAC,3:IP+MAC
返回 return string:地址串

function GetClientAddr($AiType=0) 

  $sMac = ""; 
  $ip= getenv("REMOTE_ADDR");  
  $ip1 = getenv("HTTP_X_FORWARDED_FOR");  
  $ip2 = getenv("HTTP_CLIENT_IP");  
  ($ip1) ? $ip = $ip1 : null;  
  ($ip2) ? $ip = $ip2 : null;  
  if ($AiType > 1) 
  { 
     $sCmd = "nbtstat -a " . $ip; 
     $sString = shell_exec($sCmd); 
     $aTemp = split("=",$sString); 
     $sMac = trim($aTemp[count($aTemp)-1]); 
  } 
  switch($AiType) 
  { 
    case 0: 
      $sResult = ""; 
      break; 
    case 1: 
      $sResult = $ip; 
      break; 
    case 2: 
      $sResult = $sMac; 
      break; 
    case 3: 
      $sResult = $ip . " " . $sMac; 
      break; 
    default: 
      $sResult = ""; 
      break; 
   } 
    return $sResult; 

?>    


 以上就是php获取IP、MAC地址函数的内容,更多相关内容请关注PHP中文网(www.php.cn)! 


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