Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use php to convert ip address into real address code

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to use php to convert ip addresses into real address codes". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Php translates ip addresses into real addresses: 1. Create a PHP example file; 2. Use the "define ('WEB_ROOT',dirname (_ _ FILE__)); echo convertip (' ip','full');" method to achieve the translation.

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

The method of translating IP address into Real address by php

An example of using the method is as follows

Define ('WEB_ROOT',dirname (_ _ FILE__)); echo convertip (' 111.63.244.69)

Func convertip

Function convertip ($ip,$integrity='simple') {$return=''; $integrity=in_array ($integrity,array ('simple','full'))? $integrity:'simple'; if (preg_match ("/ ^\ d {1 ip 3}\.\ d {1 iparray=explode 3} $/", $ip)) If ($iparray [0] = = 10 | $iparray [0] = = 127th | ($iparray [0] = = 192&&$iparray [1] = = 168) | ($iparray [0] = = 172iparray & ($iparray [1] > = 16&&$iparray [1] 255 | | $iparray [1] > 255 | $iparray [2] > 255 | $iparray [3] > 255) {$return='- Invalid IP Address';} else {$tinyipfile=WEB_ROOT.'./tinyipdata.dat') / / IP== address packet Lite Edition $fullipfile=WEB_ROOT.'./wry.dat';//IP== address packet complete full page if ($integrity=='simple'&&@file_exists ($tinyipfile)) {$return=convertip_tiny ($ip,$tinyipfile);} elseif (@ file_exists ($fullipfile)) {$return=convertip_full ($ip,$fullipfile) }} return $return;}

Func convertip_tiny

Function convertip_tiny ($ip,$ipdatafile) {static $fp=NULL,$offset=array (), $index=NULL; $ipdot=explode ('.', $ip); $ip=pack ('naughty journal ip2long ($ip)); $ipdot [0] = (int) $ipdot [0]; $ipdot [1] = (int) $ipdot [1]; if ($fp===NULL&&$fp=@fopen ($ipdatafile,'rb')) {$offset=@unpack (' Nlen',@fread ($fp,4)) $index=@fread ($fp,$offset ['len']-4);} elseif ($fp==FALSE) {return'-Invalid IP data file';} $length=$offset ['len']-1028; $start= @ unpack (' Vlen',$index [$ipdot [0] * 4]. $index [$ipdot [0] * 413]. $index [$ipdot [0] * 4 * 2]. $index [$ipdot [0] * 418]); for ($start=$start ['len'] * 8mm 1024) $start=$ip) {$index_offset=@unpack ('Vlen',$index {$start+4}. $index {$start+5}. $index {$start+6}. "\ x0"); $index_length=@unpack (' Clen',$index {$start+7}); break;}} @ fseek ($fp,$offset ['len'] + $index_offset [' len']-1024) If ($index_length ['len']) {return' -'. @ fread ($fp,$index_length ['len']);} else {return'-Unknown';}}

Func convertip_full

Function convertip_full ($ip,$ipdatafile) {if (! $fd=@fopen ($ipdatafile,'rb')) {return'- Invalid IP datafile';} $ip=explode ('.', $ip); $ipNum=$ip [0] * 16777216+$ip [1] * 65536+$ip [2] * 256+$ip [3]; if (! ($DataBegin=fread ($fd,4)) | |! ($DataEnd=fread ($fd,4)) return; @ $ipbegin=implode

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report