您好,登錄后才能下訂單哦!
前端 stat.js
// JavaScript Document
/**
* 瀏覽器統計腳本
*/
var xmlHttp;
var statdomain='http://www.logs.com';
var statIdName = "logId";
function setCookie(name,value)
{
var exp = new Date();
exp.setTime(exp.getTime() + 24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
//讀取cookies
function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}
//刪除cookies
function delCookie(name)
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null)
document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
/**
* 獲取當前時間戳
*/
function getTimestamp() {
var timestamp = Date.parse(new Date());
return timestamp;
}
function generRandom(){
return Math.round(Math.random() * 3000000000);
}
/**
* 生成statId
*/
function genStatId() {
var cookieId = getTimestamp();
cookieId = "cid" + "-" + cookieId + "-" + generRandom();
return cookieId;
}
/**
* 設置StatId
*/
function setStatId() {
var cookieId = genStatId();
setCookie(statIdName, cookieId);
}
/**
* 獲取StatId
*/
function getStatId() {
var statId = getCookie(statIdName);
if (statId != null && statId.length > 0) {
return statId;
} else {
setStatId();
return getStatId();
}
}
/**
* 獲取UA
*/
function getUA() {
var ua = navigator.userAgent;
if (ua.length > 250) {
ua = ua.substring(0, 250);
}
return ua;
}
function getBrower() {
var ua = getUA();
if (ua.indexOf("MSIE") >= 0) {
var ver=ua.match(/MSIE ([\d.]+)/)[1];
return "IE("+ver+")";
}
//Maxthon
else if (ua.indexOf("Maxthon") >= 0) {
var ver=ua.match(/Maxthon\/([\d.]+)/)[1];
return "Maxthon("+ver+")";
}
//firefox
else if (ua.indexOf("Firefox") >= 0) {
var ver=ua.match(/Firefox\/([\d.]+)/)[1];
return "Firefox("+ver+")";
}
//Chrome
else if(ua.indexOf("Chrome") >= 0){
var ver=ua.match(/Chrome\/([\d.]+)/)[1];
return "Chrome("+ver+")";
}
//Opera
else if(ua.indexOf("Opera") >= 0){
var ver=ua.match(/Opera.([\d.]+)/)[1];
return "Opera("+ver+")";
}
//Safari
else if(ua.indexOf("Safari") >= 0){
var ver=ua.match(/version\/([\d.]+)/)[1];
return "Safari("+ver+")";
}else{
return "ot";
}
}
/**
* 獲取瀏覽器語言
*/
function getBrowerLanguage() {
var lang = navigator.browserLanguage;
return lang != null && lang.length > 0 ? lang : "";
}
/**
* 獲取操作系統
*/
function getPlatform() {
return navigator.platform;
}
/**
* 獲取頁面title
*/
function getPageTitle() {
return document.title;
}
/**
* 構造XMLHttpRequest對象
*
* @return
*/
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
} else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
var logrand=generRandom();//同一用戶頁面記錄id
function InitLE(stat_param,isclose) {
var UA = encodeURIComponent(getUA());
var REFURL = encodeURIComponent(document.referrer);
var URL = encodeURIComponent(document.URL);
var ScreenX = screen.width;
var ScreenY = screen.height;
var OS = getPlatform();
var Brower = getBrower();
var BrowerLanguage = getBrowerLanguage();
var PageTitle = encodeURIComponent(getPageTitle());
var Action = statdomain+"/stat.gif?rd="+generRandom();
//var Action = statdomain+"/api.php?op=addlogs&rd="+generRandom();
var CookieId = getStatId();
var p = stat_param + "&isclose="+ isclose +"&stat_cookieid=" + CookieId + "&stat_randid=" + logrand +"&stat_ua=" + UA + "&stat_referurl="
+ REFURL + "&stat_url=" + URL + "&stat_os=" + OS + "&stat_browser=" + Brower + "&stat_desc=" + PageTitle;
createXMLHttpRequest();
xmlHttp.open("GET", Action+'&'+p,false);
xmlHttp.send(null); //設置為發送給服務器數據
}
InitLE(stat_param,0);
window.onbeforeunload = function(){
InitLE(stat_param,1);
}
后端php代碼
<?php
defined('IN_LOGS') or exit('No permission resources.');
ignore_user_abort(TRUE);
logs_base::load_sys_class('Log', '', 0);
$Log = Log::factory('file', ROOT_PATH . 'api' . DIRECTORY_SEPARATOR .'logs'.DIRECTORY_SEPARATOR.'addlogs'.DIRECTORY_SEPARATOR . date('Ymd') . '.log', 'Request', array('timeFormat' => '%Y-%m-%d %H:%M:%S'));
//set_time_limit(0);//取消腳本執行延時上限
$db=logs_base::load_model();
$allow_origin=array();
$sites=$db->select('site','*');
foreach($sites as $k=>$v){
$allow_origin[]=$v['siteurl'];
}
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
$Log->log('origin:' .$origin);
if(in_array($origin, $allow_origin)){
header('Access-Control-Allow-Origin:'.$origin);
header('Access-Control-Allow-Credentials:true');
}
$Log->log('SRART DATA');
$post=$_REQUEST;
header("Content-type: p_w_picpath/gif");
$imgpath=ROOT_PATH.'stat.gif';
echo file_get_contents($imgpath);
if($origin && $origin!='http://'.SITE_URL && !in_array($origin,$allow_origin)){
exit();
}
if($post){
$prex='stat_';
$Log->log('POST DATA:' .array2string($post));
$key=logs_base::load_config('system', 'auth_key');
$decode=sys_auth($post[$prex.'ld'], 'DECODE',$key);
if(!in_array($decode,$allow_origin)){
exit();//非法請求!
}
$siteid=0;
foreach($sites as $k=>$v){
if($v['siteurl']===$decode){
$siteid=$v['id'];
break;
}
}
if(!$siteid)exit();//'非法請求!'
$gifo=logs_base::load_sys_class ('get_gust_info');
$shortname=$db->get('site','shortname',array('id'=>$siteid));
if(!$shortname)exit();//沒有設置記錄表
$loglist_table='loglist_'.$shortname;
$fields=array('actid',//'功能點id'
'doids',//'操作對象id'
'dodesc',//'操作對象描述'
'system',//'操作系統'
'intime',//'進入時間'
'outtime',//'離開時間'
'standtime',//'停留時間'
'browser',//'瀏覽器',
'ip',//'ip',
'userid',//'用戶id'
'companyname',//'公司名稱'
'url',//'當前url地址'
'referurl',//'來源url'
'desc',//'描述',
'cookieid',
'randid'
);
$data=array();
foreach($fields as $field){
if(isset($post[$prex.$field]) && $post[$prex.$field])$data[$field]=$post[$prex.$field];
}
$Log->log('do table:' .$loglist_table);
if($data){
if(!isset($data['intime']) || !$data['intime'])$data['intime']=time();
if(!isset($data['system']) || !$data['system'])$data['system']=$gifo->GetOs();//操作系統
if(!isset($data['browser']) || !$data['browser'])$data['browser']=$gifo->GetBrowser();//瀏覽器類型
if(!isset($data['ip']) || !$data['ip'])$data['ip']=$gifo->Getip();
if(!isset($data['address']) || !$data['address'])$data['address']=$gifo->GetAddress();
if(!isset($data['referurl']) || !$data['referurl'])$data['referurl']=HTTP_REFERER;
if($post['isclose'] && $data['cookieid'] && $data['randid']){
$Log->log('update table:' .$data['cookieid'].'|'.$data['randid']);
$preinfo=$db->get($loglist_table,array('id','intime'),array('cookieid'=>$data['cookieid'],'randid'=>$data['randid'],'ip'=>$data['ip'],'outtime'=>0));
if($preinfo){
$outtime=time();
$standtime=$outtime-$preinfo['intime'];
$db->update($loglist_table,array('outtime'=>$outtime,'standtime'=>$standtime),array('id'=>$preinfo['id']));
$Log->log('END DATA');
exit();
}
}
$Log->log('insert DATA:' .array2string($data));
$db->insert($loglist_table,$data);
}
$Log->log('END DATA');
}
?>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。