首页 > 代码库 > 站长统计代码实现
站长统计代码实现
前端 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: image/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=http://www.mamicode.com/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‘);
}
?>
本文出自 “12425224” 博客,请务必保留此出处http://12435224.blog.51cto.com/12425224/1903445
站长统计代码实现