A-A+

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

2019年03月10日 我爱编程 暂无评论

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

  1. <?php
  2. global $count;
  3. $count=1;
  4. if(!isset($_COOKIE["visittime"])){ setcookie("visittime",date("y-m-d H:i:s"));
  5. setcookie("visitcount",1); echo "欢迎你第一次访问网站!";
  6. }
  7. else{
  8. setcookie("visittime",date("y-m-d,H:i:s"),time()+60); $count=$_COOKIE['visitcount']+1;
  9. setcookie("visitcount",$count); echo "你上次访问网站的时间为:".$_COOKIE['visittime'];
  10. echo "<br>";
  11. }
  12. echo "你第".$_COOKIE['visitcount']."访问网站的时间为:".date("y-m-d H:i:s");
  13. ?>

下面看个简单的js实现访问次数统计代码,脚本说明:第一步:把如下代码加入<body>区域中:

  1. <SCRIPT LANGUAGE="JavaScript">
  2. <!-- Begin
  3. function getCookieVal (offset) {
  4. var endstr = document.cookie.indexOf (";", offset);
  5. if (endstr == -1)
  6. endstr = document.cookie.length;
  7. return unescape(document.cookie.substring(offset, endstr));
  8. }
  9. function GetCookie (name) {
  10. var arg = name + "=";
  11. var alen = arg.length;
  12. var clen = document.cookie.length;
  13. var i = 0;
  14. while (i < clen) {
  15. var j = i + alen;
  16. if (document.cookie.substring(i, j) == arg)
  17. return getCookieVal (j);
  18. i = document.cookie.indexOf(" ", i) + 1;
  19. if (i == 0)
  20. break;
  21. }
  22. return null;
  23. }
  24. function SetCookie (name, value) {
  25. var argv = SetCookie.arguments;
  26. var argc = SetCookie.arguments.length;
  27. var expires = (2 < argc) ? argv[2] : null;
  28. var path = (3 < argc) ? argv[3] : null;
  29. var domain = (4 < argc) ? argv[4] : null;
  30. var secure = (5 < argc) ? argv[5] : false;
  31. document.cookie = name + "=" + escape (value) +
  32. ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
  33. ((path == null) ? "" : ("; path=" + path)) +
  34. ((domain == null) ? "" : ("; domain=" + domain)) +
  35. ((secure == true) ? "; secure" : "");
  36. }
  37. function DisplayInfo() {
  38. var expdate = new Date();
  39. var visit;
  40. expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 365));
  41. if(!(visit = GetCookie("visit")))
  42. visit = 0;
  43. visit++;
  44. SetCookie("visit", visit, expdate, "/"nullfalse);
  45. var message;
  46. if(visit == 1)
  47. message="         Welcome to my page!";
  48. if(visit== 2)
  49. message="           I see you came back !";
  50. if(visit == 3)
  51. message="               Oh, it's you again!";
  52. if(visit == 4)
  53. message="            You must be curious!";
  54. if(visit == 5)
  55. message="      You're practically a regular!";
  56. if(visit == 6)
  57. message="              You need a hobby!";
  58. if(visit == 7)
  59. message="             Nothing better to do?";
  60. if(visit == 8)
  61. message="            Don't you ever sleep?";
  62. if(visit == 9)
  63. message="                      Get a life!!!";
  64. if(visit >= 10)
  65. message="  Rent is due on the 1st of the month!";
  66.   alert("n"+"你的浏览器已经访问过本页" + visit +"次了"+"n"+"n"+message);
  67. }
  68. function ResetCounts() {
  69. var expdate = new Date();
  70. expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 365));
  71. visit = 0;
  72. SetCookie("visit", visit, expdate , "/"nullfalse);
  73. history.go(0);
  74. }
  75. // End -->
  76. </Script>
  77. <FORM>
  78. <CENTER>
  79. <INPUT NAME="update" TYPE="BUTTON" VALUE="查看次数" OnClick="history.go(0)">
  80. <INPUT NAME="reset" TYPE="BUTTON" VALUE="重新计数" OnClick="ResetCounts()">
  81. </CENTER>
  82. </FORM>

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录