Determine whether the user IP is on the same network segment or the same LAN instance

Source: Internet
Author: User

The code is as follows Copy Code
$start =iptoint (' 192.168.1.110 ');
$end = Iptoint (' 192.168.0.1 ');
$ipInt = Iptoint (get ());
$result = false;
if ($ipInt >= $start && $ipInt <= $end) {
$result = true;
}

if ($result)
{
Echo ' In the same net segment ';
}
Else
{
Echo ' is not in the same net segment ';
}


Not on the same network segment

/*
* IP to reshape numerical value
* @param string $ip IP
* @return int
*/

The code is as follows Copy Code
function Iptoint ($IP) {
$ips tutorial = Explode ('. ', $IP);
if (count ($ips) >=4) {
$int = $ips [0]*256*256*256+ $ips [1]*256*256+ $ips [2]*256+ $ips [3];//] based on the Ip,a,b,c class
} else {
throw new Exception (' IP is error ');
}
return $int;
}


/*
* Get User IP address 192.68.0.110
*/

The code is as follows Copy Code
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;
}
/*

Site original Reprint annotated source www.111cn.net
 */

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.