Count Visits with Cookies – Send User To Correct Page

[Total: 0    Average: 0/5]

This example counts the number of visits to this site (page).

Head Code:

<script type=text/javascript>
var TEST_ONLY = true;// set to false to not run this in test mode

function Cookie(document,name,hours,path,domain,secure) {
// any VAR in "this" that does not start with a "$" will
// be written into the cookie (read from also)
this.$doc = document
this.$name = name
if (hours) this.$expiration=new Date((new Date()).getTime()+hours*3600000); else this.$expiration = null
if (path) this.$path = path; else this.$path = null
if (domain) this.$domain = domain; else this.$domain = null
if (secure) this.$secure = true; else this.$secure = false

function CookieWrite() {
var cookieval=""
for(var prop in this) {
if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function') || prop == '') continue
if (cookieval != "") cookieval += '&'
var cookie=this.$name+"="+cookieval
if (this.$expiration) cookie+='; expires=' + this.$expiration.toGMTString()
if (this.$path) cookie+='; path=' + this.$path
if (this.$domain) cookie+='; domain=' + this.$domain
if (this.$secure) cookie+='; secure'

function CookieRead() {
var allcookies=this.$doc.cookie
if (allcookies=="") {
return false
var start= allcookies.indexOf(this.$name+'=')
if (start== -1) {
return false
start += this.$name.length+1
var end=allcookies.indexOf(';',start)
if (end == -1) end=allcookies.length
var cookieval = allcookies.substring(start,end)
var a = cookieval.split('&')
for (var i=0;i<a.length;i++) a[i]=a[i].split(':')
for (var i=0;i<a.length;i++) this[a[i][0]]=unescape(a[i][1])
return true

function CookieDelete() {
var cookie = this.$name+'='
if (this.$path) cookie+='; path='+this.$path
if (this.$domain) cookie+='; domain='+this.$domain
cookie+='; expires=Fri, 02-Jan-1970 00:00:00 GMT' // MAKE IT EXPIRE!

new Cookie()
Cookie.prototype.write = CookieWrite
Cookie.prototype.del = CookieDelete = CookieRead

//  The following script determines if this is a first time user
//  [based upon a cookie counter value]
//  If it is it makes sure that the user goes to page #1 first.
//  It checks to find out what page # the user should be on,
//  If the user is on that page it allows the rest of the page to load,
//  otherwise the user is sent to the proper page.
//  If the user is on the correct page the cookie counter is incremented
//  so that the next page the user can look at is the next # page.

var myCookie = new Cookie(document,"myMagicNumber",365); // expire in 1 year.
if (! || !myCookie.counter)
  myCookie.counter = 1; // first visit!
myCookie.write(); // store off the cookie!

var _loc = document.location.toString();
// This example assumes that the location/address of this page
// looks something like this:
//  where # is 0, 1, 2, 3, 4...
var _basePage = "";
var _pageName = "magictrick_";
var newPage = _pageName+myCookie.counter+".html";
if (_loc.indexOf(newPage)==-1) {
  // we are not on the right page - load the correct one
  if (!TEST_ONLY)
    document.location = _basePage+newPage;

// if the logic gets this far then the user is on the correct page.
function clearCookie() { myCookie.del(); }

Body Code:

  if (TEST_ONLY)
    document.write("loading page "+_basePage+newPage);  // test line only to show what it is doing..
document.write("<BR>Your cookie number/count is "+myCookie.counter+"<BR>");
<!-- this is only here to show how to do this -->
<a href="javascript:clearCookie()">clear out cookie</a>

Times Viewed: 8

Leave a Reply

Your email address will not be published. Required fields are marked *