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

/// BEGIN COOKIE CONSTRUCT
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 += '&'
cookieval+=prop+":"+escape(this[prop])
}
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'
this.$doc.cookie=cookie
}

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!
this.$doc.cookie=cookie
}

new Cookie()
Cookie.prototype.write = CookieWrite
Cookie.prototype.del = CookieDelete
Cookie.prototype.read = CookieRead
/// END COOKIE CONSTRUCT
</script>

<script>
//  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.read() || !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:
//    http://www.yoursite.com/magictrick_#.html  where # is 0, 1, 2, 3, 4...
var _basePage = "http://www.yoursite.com/";
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.
myCookie.counter++;
myCookie.write();
function clearCookie() { myCookie.del(); }
</script>

Body Code:

<script>
  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>");
</script>
<BR>
<!-- this is only here to show how to do this -->
<a href="javascript:clearCookie()">clear out cookie</a>

Times Viewed: 0

Leave a Reply

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