search
Homephp教程PHP源码判断用户ip是否在同一网段或同一局域网实例

原理根据用户给出的ip,再获取当前IP然后转换成数字型的,根据IP,a,b,c类进行计算,根据进行对比就OK

<script>ec(2);</script>

 

 代码如下 复制代码
$start =ipToInt('192.168.1.110');
 $end = ipToInt('192.168.0.1');
 $ipInt = ipToInt( get() );
 $result = false;
 if ($ipInt>=$start && $ipInt    $result = true;
 }
 
 if( $result )
 {
  echo '在同一网段';
 }
 else
 {
  echo '不在同一网段';
 }


 //不在同一网段
 
 /*
  * IP转成整形数值
  * @param string $ip IP
  * @return int
  */

 代码如下 复制代码
 function ipToInt($ip) {
  $ips教程 = explode('.',$ip);
  if (count($ips)>=4) {
   $int = $ips[0]*256*256*256+$ips[1]*256*256+$ips[2]*256+$ips[3];//根据IP,a,b,c类进行计算
  } else {
   throw new Exception('ip is error');
  }
  return $int;
 }


 /*
 * 获取用户IP地址 192.68.0.110
 */
 

 代码如下 复制代码
 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;
 }
 /*

 

本站原创转载注明来源www.111cn.net
 */

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment