统计用户访问站点次数(用户端)

统计用户访问站点次数(用户端)

通过HTML代码实现统计某一用户访问某一站点的次数:

只能在某一用户端可见,同时不能统计该站点的总被访问数

代码如下:

<script language="JavaScript">    
var caution = false    
function setCookie(name,value,expires,path,domain,secure)    
{        var curCookie = name + "=" + escape(value) +        ((expires) ? "; expires=" + expires.toGMTString() : "") +        ((path) ? "; path=" + path : "") +        ((domain) ? "; domain=" + domain : "") +        ((secure) ? "; secure" : "")        if (!caution || (name + "=" + escape(value)).length <= 4000){document.cookie = curCookie}        else if (confirm("Cookie exceeds 4KB and will be cut!")){        document.cookie = curCookie}    
}    
function getCookie(name)    
{        var prefix = name + "="        var cookieStartIndex = document.cookie.indexOf(prefix)        if (cookieStartIndex == -1)      {  return null}       var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)      if (cookieEndIndex == -1){        cookieEndIndex = document.cookie.length}        return unescape(document.cookie.substring(cookieStartIndex + prefix.length,cookieEndIndex))    
}    
function deleteCookie(name,path,domain)    
{        if (getCookie(name))        {            document.cookie = name + "=" +            ((path) ? "; path=" + path : "") +            ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT"        }    
}    
function fixDate(date)    
{        var base = new Date(0)        var skew = base.getTime()        if (skew > 0){        date.setTime(date.getTime() - skew)}    
}    
var now = new Date()    
fixDate(now)    
now.setTime(now.getTime() + 365*24*60*60*1000)    
var visits = getCookie("counter")    
if (!visits)
{    visits = 1
}    
else
{    visits = parseInt(visits) + 1
}    
setCookie("counter", visits, now)    
document.write("您已访问本站 " + visits + "次。")    
</script>

使用注意:

该代码使用时写在HTML文件的<body> ... </body>

作者的话:

这是今天看书的时候发现的一段代码,虽说是统计访问次数,但是并不能永久储存某一用户访问该站点的次数以及该站点被用户访问的总次数,只能在每次刷新网页后累加访问次数,有可能是因为我应用的站点还在开发中,只能用本地文件打开网页,所以每次重新打开网页后次数统计会归零。

统计用户访问站点次数(用户端)

统计用户访问站点次数(用户端)

通过HTML代码实现统计某一用户访问某一站点的次数:

只能在某一用户端可见,同时不能统计该站点的总被访问数

代码如下:

<script language="JavaScript">    
var caution = false    
function setCookie(name,value,expires,path,domain,secure)    
{        var curCookie = name + "=" + escape(value) +        ((expires) ? "; expires=" + expires.toGMTString() : "") +        ((path) ? "; path=" + path : "") +        ((domain) ? "; domain=" + domain : "") +        ((secure) ? "; secure" : "")        if (!caution || (name + "=" + escape(value)).length <= 4000){document.cookie = curCookie}        else if (confirm("Cookie exceeds 4KB and will be cut!")){        document.cookie = curCookie}    
}    
function getCookie(name)    
{        var prefix = name + "="        var cookieStartIndex = document.cookie.indexOf(prefix)        if (cookieStartIndex == -1)      {  return null}       var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)      if (cookieEndIndex == -1){        cookieEndIndex = document.cookie.length}        return unescape(document.cookie.substring(cookieStartIndex + prefix.length,cookieEndIndex))    
}    
function deleteCookie(name,path,domain)    
{        if (getCookie(name))        {            document.cookie = name + "=" +            ((path) ? "; path=" + path : "") +            ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT"        }    
}    
function fixDate(date)    
{        var base = new Date(0)        var skew = base.getTime()        if (skew > 0){        date.setTime(date.getTime() - skew)}    
}    
var now = new Date()    
fixDate(now)    
now.setTime(now.getTime() + 365*24*60*60*1000)    
var visits = getCookie("counter")    
if (!visits)
{    visits = 1
}    
else
{    visits = parseInt(visits) + 1
}    
setCookie("counter", visits, now)    
document.write("您已访问本站 " + visits + "次。")    
</script>

使用注意:

该代码使用时写在HTML文件的<body> ... </body>

作者的话:

这是今天看书的时候发现的一段代码,虽说是统计访问次数,但是并不能永久储存某一用户访问该站点的次数以及该站点被用户访问的总次数,只能在每次刷新网页后累加访问次数,有可能是因为我应用的站点还在开发中,只能用本地文件打开网页,所以每次重新打开网页后次数统计会归零。