IP segmentation class for IP acquisition and judgment of PHP

Source: Internet
Author: User

IP segmentation class for IP acquisition and judgment of PHP

Class Ip {

/**
* Take IP
* @return String
*/
public static function get () {
if ($_server[' http_client_ip '] && $_server

[' Http_client_ip ']!= ' unknown ') {
$ip = $_server[' http_client_ip '];
} elseif ($_server[' http_x_forwarded_for ') && $_server

[' http_x_forwarded_for ']!= ' unknown ') {
    $ip = $_server[' http_x_forwarded_for '];
  } else {
    $ip = $_server[' remote_addr ';
  }
   return $IP;
 }
 
 /**
  * IP to reshape value
  * @param string $IP IP
  * @return int
  */  public static function Iptoint ($IP) {
   $ips = Explode ('. ', $ip);
  if (Count ($ips) & gt;=4) {
    $int = $ips [0]*256*256*256+ $ips [1]*256*256+ $ips [2]

*256+ $ips [3];//calculated according to the Ip,a,b,c class
  } else {
   throw new Exception (' IP is error ');
 &NBSP}
  return $int;
 
 
 /**
  * Determine if IP is in an IP segment
  * param string $startIp Start IP
  * @param string $endIp End IP
  * @param string $IP IP
  * @return b OOL
  */
 public static function IsIn ($startIp, $endIp, $ip) {
   $start = Ip::iptoint ($sta RTIP);
   $end = Ip::iptoint ($endIp);
   $ipInt = Ip::iptoint ($IP);
   $result = false;
  if ($ipInt >= $start && $ipInt <= $end) {
    $result = true;
 & nbsp;}
  return $result;
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.