利用cookie 实现访问次数统计代码

2019-07-26 10:48栏目:编程学习

[/code]
<?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");
?>
[/code]

本教程举了两个实例说明利用cookie实现访问次数统计的代码,包括有php教程 js 两种方法

您可能感兴趣的文章:

  • PHP redis实现的悲观锁机制示例
  • PHP操作Redis常用技巧总结
  • php Session存储到Redis的方法
  • php结合redis实现高并发下的抢购、秒杀功能的实例
  • PHP实现操作redis的封装类完整实例
  • Linux下php安装Redis扩展的方法
  • php中使用redis队列操作实例代码
  • 项目中应用Redis Php的场景
  • PHP实现限制IP访问及提交次数的方法详解
  • PHP实现限制IP访问的方法
  • 用php代码限制国内IP访问我们网站
  • PHP实现redis限制单ip、单用户的访问次数功能示例

<?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, it's you again!";
if(visit == 4)
message="            You must be curious!";
if(visit == 5)
message="      You're practically a regular!";
if(visit == 6)
message="              You need a hobby!";
if(visit == 7)
message="             Nothing better to do?";
if(visit == 8)
message="            Don't 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>

版权声明:本文由威尼斯人app发布于编程学习,转载请注明出处:利用cookie 实现访问次数统计代码