PHP 记录访客的浏览信息方法


Posted in PHP onJanuary 29, 2018

可以记录访客的地理位置,操作系统,浏览器,IP,时间和访问的文件。

1.首先创建一个comm_function.php文件:

<?php
//echo $_SERVER['HTTP_USER_AGENT'];
//echo "<br />".$_SERVER ['REMOTE_ADDR'];
/**
 * 获取客户端类型,手机还是电脑,以及相应的操作系统类型。
 *
 * @param string $subject
 */
function get_os($agent) {
  $os = false;
  if (preg_match ( '/win/i', $agent ) && strpos ( $agent, '95' )) {
    $os = 'Windows 95';
  } else if (preg_match ( '/win 9x/i', $agent ) && strpos ( $agent, '4.90' )) {
    $os = 'Windows ME';
  } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/98/i', $agent )) {
    $os = 'Windows 98';
  } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 6.0/i', $agent )) {
    $os = 'Windows Vista';
  } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 6.1/i', $agent )) {
    $os = 'Windows 7';
  } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 6.2/i', $agent )) {
    $os = 'Windows 8';
  } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 10.0/i', $agent )) {
    $os = 'Windows 10'; // 添加win10判断
  } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 5.1/i', $agent )) {
    $os = 'Windows XP';
  } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt 5/i', $agent )) {
    $os = 'Windows 2000';
  } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/nt/i', $agent )) {
    $os = 'Windows NT';
  } else if (preg_match ( '/win/i', $agent ) && preg_match ( '/32/i', $agent )) {
    $os = 'Windows 32';
  } else if (preg_match ( '/linux/i', $agent )) {
    if(preg_match("/Mobile/", $agent)){
      if(preg_match("/QQ/i", $agent)){
        $os = "Android QQ Browser";
      }else{
        $os = "Android Browser";
      }
    }else{
      $os = 'PC-Linux';
    }
  } else if (preg_match ( '/Mac/i', $agent )) {
    if(preg_match("/Mobile/", $agent)){
      if(preg_match("/QQ/i", $agent)){
        $os = "IPhone QQ Browser";
      }else{
        $os = "IPhone Browser";
      }
    }else{
      $os = 'Mac OS X';
    }
  } else if (preg_match ( '/unix/i', $agent )) {
    $os = 'Unix';
  } else if (preg_match ( '/sun/i', $agent ) && preg_match ( '/os/i', $agent )) {
    $os = 'SunOS';
  } else if (preg_match ( '/ibm/i', $agent ) && preg_match ( '/os/i', $agent )) {
    $os = 'IBM OS/2';
  } else if (preg_match ( '/Mac/i', $agent ) && preg_match ( '/PC/i', $agent )) {
    $os = 'Macintosh';
  } else if (preg_match ( '/PowerPC/i', $agent )) {
    $os = 'PowerPC';
  } else if (preg_match ( '/AIX/i', $agent )) {
    $os = 'AIX';
  } else if (preg_match ( '/HPUX/i', $agent )) {
    $os = 'HPUX';
  } else if (preg_match ( '/NetBSD/i', $agent )) {
    $os = 'NetBSD';
  } else if (preg_match ( '/BSD/i', $agent )) {
    $os = 'BSD';
  } else if (preg_match ( '/OSF1/i', $agent )) {
    $os = 'OSF1';
  } else if (preg_match ( '/IRIX/i', $agent )) {
    $os = 'IRIX';
  } else if (preg_match ( '/FreeBSD/i', $agent )) {
    $os = 'FreeBSD';
  } else if (preg_match ( '/teleport/i', $agent )) {
    $os = 'teleport';
  } else if (preg_match ( '/flashget/i', $agent )) {
    $os = 'flashget';
  } else if (preg_match ( '/webzip/i', $agent )) {
    $os = 'webzip';
  } else if (preg_match ( '/offline/i', $agent )) {
    $os = 'offline';
  } else {
    $os = '未知操作系统';
  }
  return $os;
}
/**
 * 获取 客户端的浏览器类型
 * @return string
 */
function get_broswer($sys){
  if (stripos($sys, "Firefox/") > 0) {
    preg_match("/Firefox\/([^;)]+)+/i", $sys, $b);
    $exp[0] = "Firefox";
    $exp[1] = $b[1]; //获取火狐浏览器的版本号
  } elseif (stripos($sys, "Maxthon") > 0) {
    preg_match("/Maxthon\/([\d\.]+)/", $sys, $aoyou);
    $exp[0] = "傲游";
    $exp[1] = $aoyou[1];
  } elseif (stripos($sys, "MSIE") > 0) {
    preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie);
    $exp[0] = "IE";
    $exp[1] = $ie[1]; //获取IE的版本号
  } elseif (stripos($sys, "OPR") > 0) {
    preg_match("/OPR\/([\d\.]+)/", $sys, $opera);
    $exp[0] = "Opera";
    $exp[1] = $opera[1];
  } elseif(stripos($sys, "Edge") > 0) {
    //win10 Edge浏览器 添加了chrome内核标记 在判断Chrome之前匹配
    preg_match("/Edge\/([\d\.]+)/", $sys, $Edge);
    $exp[0] = "Edge";
    $exp[1] = $Edge[1];
  } elseif (stripos($sys, "Chrome") > 0) {
    preg_match("/Chrome\/([\d\.]+)/", $sys, $google);
    $exp[0] = "Chrome";
    $exp[1] = $google[1]; //获取google chrome的版本号
  } elseif(stripos($sys,'rv:')>0 && stripos($sys,'Gecko')>0){
    preg_match("/rv:([\d\.]+)/", $sys, $IE);
    $exp[0] = "IE";
    $exp[1] = $IE[1];
  }else {
    $exp[0] = "未知浏览器";
    $exp[1] = "";
  }
  return $exp[0].'('.$exp[1].')';
}
/**
 * 根据 客户端IP 获取到其具体的位置信息
 * @param unknown $ip
 * @return string
 */
function get_address_by_ip($ip) {
  $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_HEADER, 0);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $info = curl_exec($curl);
  curl_close($curl);
  return $info;
}
function clientlog() {
  $useragent = $_SERVER ['HTTP_USER_AGENT'];
  $clientip = $_SERVER ['REMOTE_ADDR'];
  $client_info = get_os ( $useragent ) . "---" . get_broswer ( $useragent );
  $rawdata_position = get_address_by_ip ( $clientip );
  $rawdata_position = json_decode($rawdata_position, true);
  $country = $rawdata_position['data']['country'];
  $province = $rawdata_position['data']['region'];
  $city = $rawdata_position['data']['city'];
  $nettype = $rawdata_position['data']['isp'];

  $time = date ( 'y-m-d h:m:s' );
  $data = "来自{$country} {$province} {$city }{$nettype} 的客户端: {$client_info},IP为:{$clientip},在{$time}时刻访问了{$_SERVER['PHP_SELF']}文件!\r\n";
  $filename = "./log.log";
  if (! file_exists ( $filename )) {
    fopen ( $filename, "w+" );
  }
  file_put_contents ( $filename, $data, FILE_APPEND );
}

2.在别的文件引入这个comm_function.php

require_once "comm_function.php";

以上这篇PHP 记录访客的浏览信息方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
php park、unpark、ord 函数使用方法(二进制流接口应用实例)
Oct 19 PHP
php通用防注入程序 推荐
Feb 26 PHP
浅析Yii中使用RBAC的完全指南(用户角色权限控制)
Jun 20 PHP
实现获取http内容的php函数分享
Feb 16 PHP
PHP中设置一个严格30分钟过期Session面试题的4种答案
Jul 30 PHP
php生成xml时添加CDATA标签的方法
Oct 17 PHP
php生成二维码时出现中文乱码的解决方法
Dec 18 PHP
如何在HTML 中嵌入 PHP 代码
May 13 PHP
PHP下载生成的csv文件及问题总结
Aug 06 PHP
thinkPHP js文件中U方法不被解析问题的解决方法
Dec 05 PHP
Laravel Eloquent分表方法并使用模型关联的实现
Nov 25 PHP
php使用event扩展的io复用测试的示例
Oct 20 PHP
laravel ORM 只开启created_at的几种方法总结
Jan 29 #PHP
PHP+Redis 消息队列 实现高并发下注册人数统计的实例
Jan 29 #PHP
PHP 使用二进制保存用户状态的实例
Jan 29 #PHP
thinkphp3.2.0 setInc方法 源码全面解析
Jan 29 #PHP
Ubuntu上安装yaf扩展的方法
Jan 29 #PHP
PHP实现的防止跨站和xss攻击代码【来自阿里云】
Jan 29 #PHP
php实现的AES加密类定义与用法示例
Jan 29 #PHP
You might like
发款php蜘蛛统计插件只要有mysql就可用
2010/10/12 PHP
PHP 显示客户端IP与服务器IP的代码
2010/10/12 PHP
让Json更懂中文(JSON_UNESCAPED_UNICODE)
2011/10/27 PHP
php程序内部post数据的方法
2015/03/31 PHP
php获取、检查类名、函数名、方法名的函数方法
2015/06/25 PHP
YiiFramework入门知识点总结(图文教程)
2015/12/28 PHP
php实现批量修改文件名称的方法
2016/07/23 PHP
PHP中使用CURL发送get/post请求上传图片批处理功能
2018/10/15 PHP
jQuery 扩展对input的一些操作方法
2009/10/30 Javascript
一个轻量级的javascript库 pj介绍
2010/12/19 Javascript
nodejs教程 安装express及配置app.js文件的详细步骤
2013/05/11 NodeJs
封装好的javascript前端分页插件pagination
2016/01/04 Javascript
轻松实现jquery手风琴效果
2016/01/14 Javascript
两种方法解决javascript url post 特殊字符转义 + &amp; #
2016/04/13 Javascript
jQuery Masonry瀑布流插件使用方法详解
2017/01/18 Javascript
老生常谈jquery id选择器和class选择器的区别
2017/02/12 Javascript
Vue.js实战之Vuex的入门教程
2017/04/01 Javascript
ES6新特性六:promise对象实例详解
2017/04/21 Javascript
vue slots 组件的组合/分发实例
2018/09/06 Javascript
在vue中使用axios实现post方式获取二进制流下载文件(实例代码)
2019/12/16 Javascript
JavaScript中的类型检查
2020/02/03 Javascript
解决vue加scoped后就无法修改vant的UI组件的样式问题
2020/09/07 Javascript
python实现得到一个给定类的虚函数
2014/09/28 Python
python实现简单登陆流程的方法
2018/04/22 Python
mac下pycharm设置python版本的图文教程
2018/06/13 Python
python生成lmdb格式的文件实例
2018/11/08 Python
Python Pandas 对列/行进行选择,增加,删除操作
2020/05/17 Python
Notino意大利:购买香水和化妆品
2018/11/14 全球购物
如何反序的迭代一个序列?how do I iterate over a sequence in reverse order
2012/02/04 面试题
经典婚礼主持词
2014/03/13 职场文书
求职信结尾怎么写
2014/05/26 职场文书
行政管理专业求职信
2014/07/06 职场文书
应届毕业生自荐信
2015/03/04 职场文书
优质护理心得体会
2016/01/22 职场文书
2019大学生预备党员转正思想汇报
2019/06/21 职场文书
解决pycharm安装scrapy DLL load failed:找不到指定的程序的问题
2021/06/08 Python