基本上目前各大网站都是用类似的方式进行访问统计,具体的方法可能不一样,但是原理是一样。都是通过js提交请求,一般是提交一个图片请求,包含到该页面中。
下面是js代码
if(typeof(pvhitimgview)=="undefined")
{
var pvhitimgview=true;
//截取dm中f字符之前的字符串
function pv_rport(dm,f)
{
var i = dm.indexOf(f);
if (i > 0) {
return dm.substring(0, i);
}
return dm;
}
//取得refUrl中的ref0参数
function getRefUrl(refUrl){
if (refUrl.indexOf('ref0') > -1){
var regexstr = /\?ref0=([\s\S]*?)$/i;
refUrl = refUrl.match(regexstr);
refUrl = encodeURI(refUrl[1]);
return refUrl;
}
}
//取得当前页面的域名信息
function getDomain()
{
hn=location.hostname;
str=hn.replace(/\.(com|net|org|cn$)\.?.*/,"");
if(str.lastIndexOf(".") == -1)
dm = "." + hn;
else
{
str = str.substring(str.lastIndexOf("."));
dm = hn.substring(hn.lastIndexOf(str));
}
return dm;
}
//读取cookie中指定name的值,如果没有指定name的cookie返回空
function readck(name){
var cookieValue = "";
var search_s = name + "=";
//如果有cookie
if(document.cookie.length > 0)
{
offset = document.cookie.indexOf(search_s);
if (offset != -1)
{
offset += search_s.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
cookieValue = unescape(document.cookie.substring(offset, end))
}
}
return cookieValue;
}
//写cookie
function writeck(name, value, hours)
{
var expire = "";
dm = getDomain();
if(hours != null)
{
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = "; expires=" + expire.toGMTString();
}
document.cookie = name + "=" + escape(value) + expire + ";domain=" + dm + ";path=/; ";
}
//获取随机数
function randck(){
return Math.floor(Math.random()*10);
}
function pv_d(){
var now = new Date().getTime();
//读取cookie中zol_userid的值
var pv_userid = readck('zol_userid');
//创建datestr随机值
var datestr=escape(now*1000+Math.round(Math.random()*1000));
datestr+="befrombj";
var imgsrc='';
//增加产品线统计
if(typeof(pv_subcatid)=="undefined")
pv_subcatid=0;
//如果页面没有被嵌套
if(top.location==self.location)
{
imgsrc='http://xxx.com/pvhit0001.gif?t='+datestr+'&subcat='+pv_subcatid+'&vuserid='+pv_userid+'&'+document.referrer;
}
if(imgsrc!='')
{
ip_ck = readck('ip_ck');
if(!ip_ck){
ip_ck = randck('ip_ck');
writeck('ip_ck',ip_ck,360);
}
imgsrc += '&ip_ck=' + ip_ck;
document.write('<img border=0 width=1 height=1 src="'+imgsrc+'">');
}
}
pv_d();
}