SoFunction
Updated on 2025-03-10

PHP gets the number and IP address instance code of the mobile phone

When we write mobile programs in PHP, we sometimes need to directly obtain the mobile phone number and corresponding IP address content. Here we have compiled the detailed and complete code content for you, and friends who need it to test it.

<?php
/**
 * Created by PhpStorm.
 * User: liubao
 * Date: 2018/8/30
 * Time: 16:21
 */
 
/**
  * Class name: mobile
  * Description: Mobile phone information category
  * Others: by chance
  */
class  mobile
{
  /**
    * Function name: getPhoneNumber
    * Function function: Get mobile phone number
    * Input parameters: none
    * Function return value: Return the number successfully, return false if failed
    * Other Instructions: Instructions
    */
  function getPhoneNumber()
  {
    if (isset($_SERVER['HTTP_X_NETWORK_INFO '])) {
      $str1 = $_SERVER['HTTP_X_NETWORK_INFO '];
      $getstr1 = preg_replace('/(.*,)(11[d])(,.*)/i ', '2 ', $str1);
      Return $getstr1;
    } elseif (isset($_SERVER['HTTP_X_UP_CALLING_LINE_ID '])) {
      $getstr2 = $_SERVER['HTTP_X_UP_CALLING_LINE_ID '];
      Return $getstr2;
    } elseif (isset($_SERVER['HTTP_X_UP_SUBNO '])) {
      $str3 = $_SERVER['HTTP_X_UP_SUBNO '];
      $getstr3 = preg_replace('/(.*)(11[d])(.*)/i ', '2 ', $str3);
      Return $getstr3;
    } elseif (isset($_SERVER['DEVICEID '])) {
      Return $_SERVER['DEVICEID '];
    } else {
      Return false;
    }
  }
 
  /**
    * Function name: getHttpHeader
    * Function function: header information
    * Input parameters: none
    * Function return value: Return the number successfully, return false if failed
    * Other Instructions: Instructions
    */
  function getHttpHeader()
  {
    $str = ' ';
    foreach ($_SERVER as $key => $val) {
      $gstr = str_replace("& ", "& ", $val);
      $str .= "$key  ->  " . $gstr . "rn ";
    }
    Return $str;
  }
 
  /**
    * Function name: getUA
    * Function function: take UA
    * Input parameters: none
    * Function return value: Return the number successfully, return false if failed
    * Other Instructions: Instructions
    */
  function getUA()
  {
    if (isset($_SERVER['HTTP_USER_AGENT '])) {
      Return $_SERVER['HTTP_USER_AGENT '];
    } else {
      Return false;
    }
  }
 
  /**
    * Function name: getPhoneType
    * Function function: Get mobile phone type
    * Input parameters: none
    * Function return value: return string successfully, return false if failed
    * Other Instructions: Instructions
    */
  function getPhoneType()
  {
    $ua = $this->getUA();
    if ($ua != false) {
      $str = explode('  ', $ua);
      Return $str[0];
    } else {
      Return false;
    }
  }
 
  /**
    * Function name: isOpera
    * Function function: determine whether it is opera
    * Input parameters: none
    * Function return value: return string successfully, return false if failed
    * Other Instructions: Instructions
    */
  function isOpera()
  {
    $uainfo = $this->getUA();
    if (preg_match('/.*Opera.*/i ', $uainfo)) {
      Return true;
    } else {
      Return false;
    }
  }
 
  /**
    * Function name: isM3gate
    * Function function: determine whether it is m3gate
    * Input parameters: none
    * Function return value: return string successfully, return false if failed
    * Other Instructions: Instructions
    */
  function isM3gate()
  {
    $uainfo = $this->getUA();
    if (preg_match('/M3Gate/i ', $uainfo)) {
      Return true;
    } else {
      Return false;
    }
  }
 
  /**
    * Function name: getHttpAccept
    * Function function: Get HA
    * Input parameters: none
    * Function return value: return string successfully, return false if failed
    * Other Instructions: Instructions
    */
  function getHttpAccept()
  {
    if (isset($_SERVER['HTTP_ACCEPT '])) {
      Return $_SERVER['HTTP_ACCEPT '];
    } else {
      Return false;
    }
  }
 
  /**
    * Function name: getIP
    * Function function: Obtain mobile phone IP
    * Input parameters: none
    * Function return value: successfully returned string
    * Other Instructions: Instructions
    */
  function getIP()
  {
    $ip = getenv('REMOTE_ADDR ');
    $ip_ = getenv('HTTP_X_FORWARDED_FOR ');
    if (($ip_ != " ") && ($ip_ != "unknown ")) {
      $ip = $ip_;
    }
    return $ip;
  }
}
 
?>

The above is all about obtaining mobile phone numbers and real-time IP in this article. Thank you for your support.