EarthWeb
Developer.com
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Book
thinking in java
 
Interact
forum
guest book
jobs
jokes
what's new

share code
 
Resource
add resource
modify resource
new resource
 

[Internet Jobs]
-----
Java by E-mail:

Get the weekly e-mail highlights on Java!
-----

-

Use COOKIE to count the user access to a page


Author: Real Gagnon
Author's WebSite: http://tactika.com/realhome/realhome.html


 <BODY ONLOAD="doCookie()">

 <SCRIPT>
 cookieName = "Counter";
 function doCookie() {

   var today = new Date()
   var expires = new Date()
   // valid for 1 year
   expires.setTime(today.getTime() + 24*365*3600000)

   if (document.cookie) {
      index = document.cookie.indexOf(cookieName);
      } 
    else { 
      index = -1;
      }
   if (index == -1) {
      document.cookie=cookieName+"=1; " + 
            " expires=" + expires.toGMTString() ;
      } 
   else {
      countbegin = (document.cookie.indexOf("=", index) + 1);
      countend = document.cookie.indexOf(";", index);
      if (countend == -1) {
         countend = document.cookie.length;
         }
      count = eval(document.cookie.substring(countbegin, countend)) + 1;
      document.cookie=cookieName+"="+count+"; " +
            " expires=" + expires.toGMTString();

      }
   }

 function getTimes() {
   count=0;
   if(document.cookie) {
     index = document.cookie.indexOf(cookieName);
     if (index != -1) {
        countbegin = (document.cookie.indexOf("=", index) + 1);
        countend = document.cookie.indexOf(";", index);
        if (countend == -1) {
           countend = document.cookie.length;
           }
        count = document.cookie.substring(countbegin, countend);
        }
     }
   return(count);
   }
 document.write("You have accessed this page <b>"+getTimes()+"</b> times.");

 if (getTimes()==0) {
    document.write("Welcome! This is your first time here");
    } 
 </SCRIPT>



Posted On: 10-Jul-1999

internet.commerce