PHP&MySQL实现访客统计功能
本文主要实现的功能是将访客的IP地址与访问时间记录存入数据库,并通过新浪IP地址查询接口,记录访客IP所在地。
记录IP
建立函数
$link=new mysqli("localhost","root","","DATABASE_name");
function visitorcount(){
global $link;
date_default_timezone_set('PRC');//设置时区,否则得到的时间不是北京时间
$ip=$_SERVER["REMOTE_ADDR"];
$date=date('Y-m-d H:i:s');
$location_one= @file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip="."$ip");//这行是新浪IP地址接口使用方法;
//在这儿以$IP="27.192.0.0";做测试;
//执行这一行后返回的内容:var remote_ip_info = //{"ret":1,"start":-1,"end":-1,"country":"\u4e2d\u56fd","province":"\u6c5f\u82cf","city":"\u5357\u4eac","district":"","isp":"","type":"","desc":""};
//"\u4e2d\u56fd、\u6c5f\u82cf、\u5357\u4eac"是经过Unicode编码后的文字,分别代表中国、山东、 潍坊;
//接着使用substr来截取大括号以及括号里的内容,用json_decode来解码
$location_substr= substr($location_one,20,-1);
$location=json_decode($location_substr,true);
$country=$location['country'];
$province=