首頁 >後端開發 >php教程 >PHP JS Ip位址及網域名稱檢測代碼_PHP教學課程

PHP JS Ip位址及網域名稱檢測代碼_PHP教學課程

WBOY
WBOY原創
2016-07-21 16:13:011087瀏覽

PHP IP 位址格式偵測函數

複製程式碼程式碼如下:

function checkIp($ip){
"", $ip);
    $ip = strtolower($ip);
    $ip= str_replace("http://", "", $ip);
    $ip= str_replace(" https://", "", $ip);
    if(preg_match('/[A-Za-z_-]/',$ip)){
        if(preg_match('/^([ w-] .) ((com)|(net)|(org)|(gov.cn)|(info)|(cc)|(com.cn)|(net.cn)|(org.cn)| (名稱)|(biz)|(tv)|(cn)|(mobi)|(名稱)|(sh)|(ac)|(io)|(tw)|(com.tw)|(hk)| (com.hk)|(ws)|(旅行)|(us)|(tm)|(la)|(me.uk)|(org.uk)|(ltd.uk)|(plc.uk)| (in)|(eu)|(it)|(jp)|(co)|(me)|(mx)|(ca)|(ag)|(com.co)|(net.co)|(nom .co)|(com.ag)|(net.ag)|(fr)|(org.ag)|(am)|(亞洲)|(at)|(be)|(bz)|(com.bz )|(net.bz)|(net.br)|(com.br)|(de)|(es)|(com.es)|(nom.es)|(org.es)|(fm)| (gs)|(co.in)|(firm.in)|(gen.in)|(ind.in)|(net.in)|(org.in)|(職位)|(ms)|(com .mx)|(nl)|(nu)|(co.nz)|(net.nz)|(org.nz)|(tc)|(tk)|(org.tw)|(idv.tw)| (co.uk)|(vg)|(ad)|(ae)|(af)|(ai)|(al)|(an)|(ao)|(aq)|(ar)|(as)| (au)|(aw)|(az)|(ba)|(bb)|(bd)|(bf)|(bg)|(bh)|(bi)|(bj)|(bm)|(bn )|(bo)|(br)|(bs)|(bt)|(bv)|(bw)|(by)|(cd)|(cf)|(cg)|(ch)|(ci)| (ck)|(cl)|(cm)|(cr)|(cu)|(cv)|(cx)|(cy)|(cz)|(dj)|(dk)|(dm)|(do )|(dz)|(ec)|(ee)|(eg)|(er)|(et)|(fi)|(fj)|(fk)|(fo)|(ga)|(gd)| (ge)|(gf)|(gg)|(gh)|(gi)|(gl)|(gm)|(gn)|(gp)|(gq)|(gr)|(gt)|(gu )|(gw)|(gy)|(hm)|(hn)|(hr)|(ht)|(hu)|(id)|(ie)|(il)|(im)|(iq)| (ir)|(is)|(je)|(jm)|(jo)|(ke)|(kg)|(kh)|(ki)|(km)|(kn)|(kr)|(kw )|(ky)|(kz)|(lb)|(lc)|(li)|(lk)|(lr)|(ls)|(lt)|(lu)|(lv)|(ly)| (ma)|(mc)|(md)|(mg)|(mh)|(mk)|(ml)|(mm)|(mn)|(mo)|(mp)|(mq)|(mr )|(mt)|(mu)|(mv)|(mw)|(my)|(mz)|(na)|(nc)|(ne)|(nf)|(ng)|(ni)| (no)|(np)|(nr)|(nz)|(om)|(pa)|(pe)|(pf)|(pg)|(ph)|(pk)|(pl)|(pm )|(pn)|(pr)|(ps)|(pt)|(pw)|(py)|(qa)|(re)|(ro)|(ru)|(rw)|(sa)| (sb)|(sc)|(sd)|(se)|(sg)|(si)|(sk)|(sl)|(sm)|(sn)|(sr)|(st)|(sv )|(sy)|(sz)|(td)|(tf)|(tg)|(th)|(tj)|(tl)|(tn)|(to)|(tr)|(tt)| (tz)|(ua)|(ug)|(uk)|(uy)|(uz)|(va)|(vc)|(ve)|(vi)|(vn)|(vu)|(wf )|(ye)|(yt)|(yu)|(za)|(zm)|(zw))$/i',$ip)){
            >            回傳false;
        }
    }其他{

        $ipArray = Explode(".", $ip);        $j = count($ipArray);

            return false;
        }
        for( $i=0;$i
if(strlen($ iparray [$ i])== 0 || $ iparray [$ i]> 255){
return false; }
}

}

} return true;
    }

}


JS程式碼:



複製程式碼

程式碼如下:

function checkIP(){
var ipArray,ip,j;
ip = document.ipform.ip.value;

if (ip.indexOf(" ")>=0){
ip = ip.replace(/ /g,"");
document.ipform.ip.value = ip;
}
if (ip.toLowerCase().indexOf("http://")==0){
ip = ip.slice(7);
document.ipform.ip.value = ip ;
}
if (ip.toLowerCase().indexOf("https://")==0){
ip = ip.slice(8);
document.ipform. ip.value = ip;
}
if (ip.slice(ip.length-1)=="/"){
ip = ip.slice(0,ip.length-1);
document.ipform.ip.value = ip;
}

if(/[A-Za-z_-]/.test(ip)){
if(!/^([w-] .) ((com)|(net)|(org)| (gov.cn)|(info)|(cc)|(com.cn)|(net.cn)|(org.cn)|(name)|(biz)|(tv)|(cn)|(mobi )|(姓名)|(sh)|(ac)|(io)|(tw)|(com.tw)|(hk)|(com.hk)|(ws)|(旅行)|(us)| (tm)|(la)|(me.uk)|(org.uk)|(ltd.uk)|(plc.uk)|(in)|(eu)|(it)|(jp)|(co )|(me)|(mx)|(ca)|(ag)|(com.co)|(net.co)|(nom.co)|(com.ag)|(net.ag)|(fr )|(org.ag)|(am)|(亞洲)|(at)|(be)|(bz)|(com.bz)|(net.bz)|(net.br)|(com.br )|(de)|(es)|(com.es)|(nom.es)|(org.es)|(fm)|(gs)|(co.in)|(firm.in)|(gen .in)|(ind.in)|(net.in)|(org.in)|(工作)|(ms)|(com.mx)|(nl)|(nu)|(co.nz)| (net.nz)|(org.nz)|(tc)|(tk)|(org.tw)|(idv.tw)|(co.uk)|(vg)|(ad)|(ae)| (af)|(ai)|(al)|(an)|(ao)|(aq)|(ar)|(as)|(au)|(aw)|(az)|(ba)|(bb )|(bd)|(bf)|(bg)|(bh)|(bi)|(bj)|(bm)|(bn)|(bo)|(br)|(bs)|(bt)| (bv)|(bw)|(by)|(cd)|(cf)|(cg)|(ch)|(ci)|(ck)|(cl)|(cm)|(cr)|(cu )|(cv)|(cx)|(cy)|(cz)|(dj)|(dk)|(dm)|(do)|(dz)|(ec)|(ee)|(eg)| (er)|(et)|(fi)|(fj)|(fk)|(fo)|(ga)|(gd)|(ge)|(gf)|(gg)|(gh)|(gi )|(gl)|(gm)|(gn)|(gp)|(gq)|(gr)|(gt)|(gu)|(gw)|(gy)|(hm)|(hn)| (hr)|(ht)|(hu)|(id)|(ie)|(il)|(im)|(iq)|(ir)|(is)|(je)|(jm)|(jo )|(ke)|(kg)|(kh)|(ki)|(km)|(kn)|(kr)|(kw)|(ky)|(kz)|(lb)|(lc)| (li)|(lk)|(lr)|(ls)|(lt)|(lu)|(lv)|(ly)|(ma)|(mc)|(md)|(mg)|(mh )|(mk)|(ml)|(mm)|(mn)|(mo)|(mp)|(mq)|(mr)|(mt)|(mu)|(mv)|(mw)| (my)|(mz)|(na)|(nc)|(ne)|(nf)|(ng)|(ni)|(no)|(np)|(nr)|(nz)|(om )|(pa)|(pe)|(pf)|(pg)|(ph)|(pk)|(pl)|(pm)|(pn)|(pr)|(ps)|(pt)| (pw)|(py)|(qa)|(re)|(ro)|(ru)|(rw)|(sa)|(sb)|(sc)|(sd)|(se)|(sg )|(si)|(sk)|(sl)|(sm)|(sn)|(sr)|(st)|(sv)|(sy)|(sz)|(td)|(tf)| (tg)|(th)|(tj)|(tl)|(tn)|(to)|(tr)|(tt)|(tz)|(ua)|(ug)|(uk)|(uy )|(uz)|(va)|(vc)|(ve)|(vi)|(vn)|(vu)|(wf)|(ye)|(yt)|(yu)|(za)| (zm)|(zw))$/i.test(ip)){
alert("不是正確的網域名稱");
document.ipform.ip.focus();
回傳 false;
}
}
else{
ipArray = ip.split(".");
j = ipArray.length
if(j!=4)
{
alert("不是正確的IP");
document.ipform.ip.focus();
回傳false;
}

for(var i=0;i{
if(ipArray[i].length==0 || ipArray[i]>255)
{
Alert("不是正確的IP");
document.ipform.ip.focus();
回傳false;
}
}
}
}



注意:上面的函數單行很長,大家複製後注意不要分行了。

http://www.bkjia.com/PHPjc/313562.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313562.htmlTechArticlePHP IP位址格式偵測函數複製程式碼如下: function checkIp($ip){ $ip = str_replace(" " , "", $ip); $ip = strtolower($ip); $ip= str_replace("http://", "", $ip); $ip=...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn