$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