php 记录PC电脑软件用户使用记录

180it 2020-02-20 AM 346℃ 0条
<?php

$ip=getClientIp();
$str =$ip.' '.get_iplocation($ip)." JQM22555555 JHM87878844"; 


write_log($str);



/**
 * 获取客户端IP地址
 *
 * @return NULL | string
 */
function getClientIp()
{
    $ip = null;

    if ($ip !== null) {
        return $ip;
    }
    if (isset($_SERVER)) {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            foreach ($arr as $ip) {
                $ip = trim($ip);
                if ($ip != 'unknown') {
                    $ip = $ip;
                    break;
                }
            }
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['HTTP_CDN_SRC_IP'])) {
            $ip = $_SERVER['HTTP_CDN_SRC_IP'];
        } else {
            if (isset($_SERVER['REMOTE_ADDR'])) {
                $ip = $_SERVER['REMOTE_ADDR'];
            } else {
                $ip = '0.0.0.0';
            }
        }
    } else {
        if (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } elseif (getenv('HTTP_CDN_SRC_IP')) {
            $ip = getenv('HTTP_CDN_SRC_IP');
        } else {
            $ip = getenv('REMOTE_ADDR');
        }
    }
    $matches = [];
    preg_match("/[\d\.]{7,15}/", $ip, $matches);
    $ip = ! empty($matches[0]) ? $matches[0] : '0.0.0.0';

    return $ip;
}



/**
 * [write_log 写入日志]
 * @param  [type] $data [写入的数据]
 * @return [type]       [description]
 */
function write_log($data){ 
    $years = date('Y-m');
    //设置路径目录信息
    $url = './log/'.$years.'/'.date('Ymd').'_request_log.txt';  
    $dir_name=dirname($url);
      //目录不存在就创建
      if(!file_exists($dir_name))
      {
        //iconv防止中文名乱码
       $res = mkdir(iconv("UTF-8", "GBK", $dir_name),0777,true);
      }
      $fp = fopen($url,"a");//打开文件资源通道 不存在则自动创建       
    fwrite($fp,date("Y-m-d H:i:s").var_export($data,true)."\r\n");//写入文件
    fclose($fp);//关闭资源通道
}



//根据ip获得访客所在地地名
function get_iplocation($ip){ 
$mip = file_get_contents("https://whois.pconline.com.cn/ip.jsp?ip=".$ip); //太平洋电脑网接口
$mip = iconv("gbk","utf-8",$mip);
$str = str_replace(PHP_EOL, '', $mip); 
$str = str_replace(array("\r\n", "\r", "\n"), "", $str);  
return  $str;
}
?>
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php 记录PC电脑软件用户使用记录