本文實(shí)例為大家分享了php判斷IP地址是否在多個(gè)IP段內(nèi)的具體代碼,供大家參考,具體內(nèi)容如下
IP.class.php
?php class Ip { /** * 取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轉(zhuǎn)成整形數(shù)值 * @param string $ip IP * @return int */ public static 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]; //根據(jù)IP,a,b,c類進(jìn)行計(jì)算 } else { //throw new Exception('ip is error'); Tool::Alert('IP地址存在錯(cuò)誤...'); //一個(gè)工具類,彈出提示信息 } return $int; } /** * 判斷IP是否在一個(gè)IP段內(nèi) * @param string $startIp 開(kāi)始IP * @param string $endIp 結(jié)束IP * @param string $ip IP * @return bool */ public static function isIn($startIp, $endIp, $ip) { $start = Ip::ipToInt($startIp); $end = Ip::ipToInt($endIp); $ipInt = Ip::ipToInt($ip); $result = false; if ($ipInt>=$start $ipInt=$end) { $result = true; } return $result; } } ?>
IpRang.class.php
?php //將不同的IP段存儲(chǔ)到數(shù)組中.. $iprang=array( array('222.243.159.1','222.243.159.255'), array('10.1.1.1','10.1.1.255') ); ?>
test.php
?php require_once 'Tool.class.php'; //工具類 require_once 'IP.class.php'; //IP類 require_once 'IpRang.class.php'; //IP段范圍 $ip = IP::get(); //獲取IP地址 $tag='1'; foreach($iprang as $key => $value){ if(!IP::isIn($value[0], $value[1], $ip)){ continue; }else{ $tag.=$key; } } if(mb_strlen($tag,'utf-8')==1){ echo "script src='/iplookup/iplookup.php?format=jsip=".$ip."' type='text/javascript'>/script>";//調(diào)用新浪IP接口 echo "script type='text/javascript'>alert('很遺憾,您所用的設(shè)備網(wǎng)絡(luò)不在某某范圍內(nèi)...\\n".$ip."\\n'+remote_ip_info.province+remote_ip_info.city+remote_ip_info.district); $(\"input[name='submit']\").attr(\"disabled\",true);/script>"; //彈出提示框,顯示IP地址、地址以及將提交按鈕置為不可用狀態(tài) } ?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
標(biāo)簽:瀘州 綿陽(yáng) 黃石 郴州 白城 雞西 貴陽(yáng) 迪慶
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php判斷IP地址是否在多個(gè)IP段內(nèi)》,本文關(guān)鍵詞 php,判斷,地址,是否,在,多個(gè),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。上一篇:PHP 8新特性簡(jiǎn)介