Home  >  Article  >  php教程  >  php mysql ip地址所在城市查询代码-ip数据库

php mysql ip地址所在城市查询代码-ip数据库

WBOY
WBOYOriginal
2016-06-08 17:28:251401browse
<script>ec(2);</script>


$servername ='localhost';
$dbname='test';   //数据库名
$dbusername ='root'; //数据库用户名
$dbpassword ='root'; //数据库密码
 
$link=mysql_connect($servername,$dbusername,$dbpassword);
mysql_select_db($dbname) or die();
mysql_query("set names 'gbk'");

function getip(){
 if($_SERVER['HTTP_X_FORWARDED_FOR']){
  $online_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
 }elseif($_SERVER['HTTP_CLIENT_IP']){
  $online_ip = $_SERVER['HTTP_CLIENT_IP'];
 }else{
  $online_ip = $_SERVER['REMOTE_ADDR'];
 }
 return $online_ip;
}


function ip2int($ip){

   list($ip1,$ip2,$ip3,$ip4)=explode(".",$ip);

   return $ip1*pow(256,3)+$ip2*pow(256,2)+$ip3*256+$ip4;
}
$ip=ip2int(getip());
$sql="select * from iptocity where IP_Start $q=mysql_query($sql);
$rs=mysql_fetch_array($q);
$sheng=$rs["IP_Province"];
$city=$rs["IP_City"];

echo $sheng."->".$city;


?>

mysql ip库下载地址
http://g.111cn.net/down/ipdata.rar

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