PHP简单日志文件

<?php
error_reporting(0);
$filename = date('Y-m-d');
$access_time = date('Y-m-d H:i:s');
file_put_contents('logo/'.$filename.'.log', '访问时间:'.$access_time."\n访问URL:".curPageURL()."\n访问IP:".get_real_ip()."\n访问来源:".$_SERVER['HTTP_REFERER']."\n访问设备:".$_SERVER['HTTP_USER_AGENT']."\n", FILE_APPEND);

function curPageURL() 
{
  $pageURL = 'http';
  if ($_SERVER["HTTPS"] == "on") 
  {
    $pageURL .= "s";
  }
  $pageURL .= "://";
 
  if ($_SERVER["SERVER_PORT"] != "80") 
  {
    $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
  } 
  else
  {
    $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
  }
  return $pageURL;
}

function get_real_ip()
{
    $ip=FALSE;
    //客户端IP 或 NONE
    if(!empty($_SERVER["HTTP_CLIENT_IP"])){
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    }
    //多重代理服务器下的客户端真实IP地址(可能伪造),如果没有使用代理,此字段为空
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
        if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
        for ($i = 0; $i < count($ips); $i++) {
            if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
                $ip = $ips[$i];
                break;
            }
        }
    }
    //客户端IP 或 (最后一个)代理服务器 IP
    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
?>


<script>
console.log("访问时间:<?php echo date('Y-m-d H:i:s')?>")
console.log("访问URL: <?php echo curPageURL() ; ?>")
console.log("访问IP: <?php echo get_real_ip() ; ?>")
console.log("访问来源: <?php echo $_SERVER['HTTP_REFERER'] ; ?>")
console.log("访问设备: <?php echo $_SERVER['HTTP_USER_AGENT'] ; ?>")
console.log("访问地区:"+returnCitySN.cip+" | "+returnCitySN.cname)

var referrer = "<?php echo $_SERVER['HTTP_REFERER'] ; ?>"
var access_ip = returnCitySN.cip+" | "+returnCitySN.cname;
var user_agent = "<?php echo $_SERVER['HTTP_USER_AGENT'] ; ?>";
</script>

  


  

posted on 2021-11-22 14:13  Ben丶大壮  阅读(92)  评论(0编辑  收藏  举报

导航