程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 利用cookie與JS 實現訪問次數統計代碼

利用cookie與JS 實現訪問次數統計代碼

編輯:關於PHP編程

global $count;

$count=1;

if(!isset($_COOKIE["visittime"])){ setcookie("visittime",date("y-m-d H:i:s"));

setcookie("visitcount",1); echo "歡迎你第一次訪問網站!";

}

else{

setcookie("visittime",date("y-m-d,H:i:s"),time()+60); $count=$_COOKIE[visitcount]+1;

setcookie("visitcount",$count); echo "你上次訪問網站的時間為:".$_COOKIE[visittime];

echo "<br>";

}
echo "你第".$_COOKIE[visitcount]."訪問網站的時間為:".date("y-m-d H:i:s");

?>

下面看個簡單的js實現訪問次數統計代碼

腳本說明:
第一步:把如下代碼加入<body>區域中

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0)
break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (2 < argc) ? argv[2] : null;
var path = (3 < argc) ? argv[3] : null;
var domain = (4 < argc) ? argv[4] : null;
var secure = (5 < argc) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DisplayInfo() {
var expdate = new Date();
var visit;
expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 365));
if(!(visit = GetCookie("visit")))
visit = 0;
visit++;
SetCookie("visit", visit, expdate, "/", null, false);
var message;
if(visit == 1)
message="         Welcome to my page!";
if(visit== 2)
message="           I see you came back !";
if(visit == 3)
message="               Oh, its you again!";
if(visit == 4)
message="            You must be curious!";
if(visit == 5)
message="      Youre practically a regular!";
if(visit == 6)
message="              You need a hobby!";
if(visit == 7)
message="             Nothing better to do?";
if(visit == 8)
message="            Dont you ever sleep?";
if(visit == 9)
message="                      Get a life!!!";
if(visit >= 10)
message="  Rent is due on the 1st of the month!";
  alert("n"+"你的浏覽器已經訪問過本頁" + visit +"次了"+"n"+"n"+message);
}
function ResetCounts() {
var expdate = new Date();
expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 365));
visit = 0;
SetCookie("visit", visit, expdate , "/", null, false);
history.go(0);
}
// End -->
</Script>
<FORM>
<CENTER>
<INPUT NAME="update" TYPE="BUTTON" VALUE="查看次數" OnClick="history.go(0)">
<INPUT NAME="reset" TYPE="BUTTON" VALUE="重新計數" OnClick="ResetCounts()">
</CENTER>
</FORM>


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved