教你写网站记录访客信息功能!(日志调试 PHP取出json数据 )

记录访客的IP所在地
浏览器类型网络类型
以及访问时间和访问地点
还有访问设备

首先涉及几个重要的知识点

1.php取出json的数据
现在有一个json数据
$json = { ["resultcode"]=> string(3) "200" ["reason"]=> string(12) "查询成功" ["result"]=> object(stdClass)#2 (2) { ["area"]=> string(8) "内网IP" ["location"]=> string(8) "内网IP" } ["error_code"]=> int(0) }
//取出result下面的area数据
$city = $arr->result->area;
2.接口的使用
3.日志调试(没有log.txt文件时会自动创建)
在微信开发时常用到的调试办法
    $stream = fopen("log.txt", "a");
    fwrite($stream, "\n" .'所在城市:' . $city);
<?
date_default_timezone_set('Asia/Shanghai');
$user_agent = $_SERVER['HTTP_USER_AGENT'];//获取手机型号
    if (stripos($user_agent, "iPhone")!==false) {
        $brand = 'iPhone';
    } else if (stripos($user_agent, "SAMSUNG")!==false || stripos($user_agent, "Galaxy")!==false || strpos($user_agent, "GT-")!==false || strpos($user_agent, "SCH-")!==false || strpos($user_agent, "SM-")!==false) {
        $brand = '三星';
    } else if (stripos($user_agent, "Huawei")!==false || stripos($user_agent, "Honor")!==false || stripos($user_agent, "H60-")!==false || stripos($user_agent, "H30-")!==false) {
        $brand = '华为';
    } else if (stripos($user_agent, "Lenovo")!==false) {
        $brand = '联想';
    } else if (strpos($user_agent, "MI-ONE")!==false || strpos($user_agent, "MI 1S")!==false || strpos($user_agent, "MI 2")!==false || strpos($user_agent, "MI 3")!==false || strpos($user_agent, "MI 4")!==false || strpos($user_agent, "MI-4")!==false) {
        $brand = '小米';
    } else if (strpos($user_agent, "HM NOTE")!==false || strpos($user_agent, "HM201")!==false) {
        $brand = '红米';
    } else if (stripos($user_agent, "Coolpad")!==false || strpos($user_agent, "8190Q")!==false || strpos($user_agent, "5910")!==false) {
        $brand = '酷派';
    } else if (stripos($user_agent, "ZTE")!==false || stripos($user_agent, "X9180")!==false || stripos($user_agent, "N9180")!==false || stripos($user_agent, "U9180")!==false) {
        $brand = '中兴';
    } else if (stripos($user_agent, "OPPO")!==false || strpos($user_agent, "X9007")!==false || strpos($user_agent, "X907")!==false || strpos($user_agent, "X909")!==false || strpos($user_agent, "R831S")!==false || strpos($user_agent, "R827T")!==false || strpos($user_agent, "R821T")!==false || strpos($user_agent, "R811")!==false || strpos($user_agent, "R2017")!==false) {
        $brand = 'OPPO';
    } else if (strpos($user_agent, "HTC")!==false || stripos($user_agent, "Desire")!==false) {
        $brand = 'HTC';
    } else if (stripos($user_agent, "vivo")!==false) {
        $brand = 'vivo';
    } else if (stripos($user_agent, "K-Touch")!==false) {
        $brand = '天语';
    } else if (stripos($user_agent, "Nubia")!==false || stripos($user_agent, "NX50")!==false || stripos($user_agent, "NX40")!==false) {
        $brand = '努比亚';
    } else if (strpos($user_agent, "M045")!==false || strpos($user_agent, "M032")!==false || strpos($user_agent, "M355")!==false) {
        $brand = '魅族';
    } else if (stripos($user_agent, "DOOV")!==false) {
        $brand = '朵唯';
    } else if (stripos($user_agent, "GFIVE")!==false) {
        $brand = '基伍';
    } else if (stripos($user_agent, "Gionee")!==false || strpos($user_agent, "GN")!==false) {
        $brand = '金立';
    } else if (stripos($user_agent, "HS-U")!==false || stripos($user_agent, "HS-E")!==false) {
        $brand = '海信';
    } else if (stripos($user_agent, "Nokia")!==false) {
        $brand = '诺基亚';
    } else {
        $brand = '电脑访问';
    }

 if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Edge')){
        $llq = 'Edge浏览器';
      }else if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')){
        $llq = '火狐浏览器';
      }elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Chrome')){
        $llq = '谷歌浏览器';
      }elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Safari')){
        $llq = '苹果Safari';
      }elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Opera')){
       $llq = '欧朋浏览器';
      }elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'360SE')){
        $llq = '360浏览器';
      }elseif(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessage')){
       $llq = '微信浏览器';
      }else{
          $llq = '其他浏览器';
      }
    // echo $brand;
    $ip=$_SERVER["REMOTE_ADDR"];//获取当前ip
    // echo $ip;

    // echo json_encode($brand,JSON_UNESCAPED_UNICODE);

    function getip()
    {

//配置您申请的appkey
$appkey = "fdcd5279cb7c7834bec7f75c119737df";

function juhecurl($url,$params=false,$ispost=0){
  $httpInfo = array();
  $ch = curl_init();

  curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
  curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
  curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
  curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  if( $ispost )
  {
      curl_setopt( $ch , CURLOPT_POST , true );
      curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
      curl_setopt( $ch , CURLOPT_URL , $url );
  }
  else
  {
      if($params){
          curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
      }else{
          curl_setopt( $ch , CURLOPT_URL , $url);
      }
  }
  $response = curl_exec( $ch );
  if ($response === FALSE) {
      //echo "cURL Error: " . curl_error($ch);
      return false;
  }
  $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
  $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
  curl_close( $ch );
  return $response;
}
//************1.根据IP/域名查询地址************
$url = "http://apis.juhe.cn/ip/ip2addr";
$ip=$_SERVER["REMOTE_ADDR"];//获取当前ip
$params = array(
    "ip" => $ip,//需要查询的IP地址或域名
    "key" => $appkey,//应用APPKEY(应用详细页查询)
    "dtype" => "",//返回数据的格式,xml或json,默认json
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
// $result = json_decode($content,true);
if($content){
    //  json_decode($content,true);
    $arr = json_decode($content);
    $city = $arr->result->area;
    $stream = fopen("log.txt", "a");
    fwrite($stream, "\n" .'---所在城市:' . $city);
}else{
  echo "请求失败";
}
    }
getip();
$time = date("Y年m月d日 H:i:s",time());
$stream = fopen("log.txt", "a");
fwrite($stream, "\n" . '访问设备为 :' . $brand ."\n".'IP地址为:'.$ip."\n".'访问时间:'.$time."\n".'用户浏览器:'.$llq."\n");
echo date('Y-m-d H:i:s');die;

为您推荐

评论已关闭