php mysql ip地址所在城市查询代码-ip数据库
<?
$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<='$ip' order by IP_Start desc limit 1";
$q=mysql_query($sql);
$rs=mysql_fetch_array($q);
$sheng=$rs["IP_Province"];
$city=$rs["IP_City"];
echo $sheng."->".$city;
?>
mysql ip库下载地址
http://www.phprm.com
本文链接:http://www.phprm.com/code/php-mysql-ip.html
收藏随意^^请保留教程地址.