Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Let the user know when they last visited your web site

Cookies

Let the user know when they last visited your web site

by  adrikev  Posted    (Edited  )
So, you want the user to know when they last visited your web site. This can be done using cookies (yum). This is how it works:

When a user makes a first visit to your web site he/she will see a message that says This is your first visit here. Then the script will write a cookie on the users machine that contains todays date and time. When the user returns to your site the script will read the cookie, return a message to the browser and overwrite the cookie with a new one that contains todays date and time. The new message will read:

Your last visit was on:
weekday month day at time PM/AM, year


Is this great or what? Here is how it is done.
[color red]
Code:
<script language="JavaScript">
<!--
readCookie();

function readCookie() {
     if (document.cookie == "") {
	writeCookie();
        alertMessage();
     } else {
	var the_cookie = document.cookie;
	the_cookie = unescape(the_cookie);
	the_cookie_split = the_cookie.split(";");
	for (loop=0;loop<the_cookie_split.length;loop++) {
		var part_of_split = the_cookie_split[loop];
		var find_name = part_of_split.indexOf("nfti_date")
		if (find_name!=-1) {
			break;
		} // Close if
	} // Close for
	if (find_name==-1) {
		writeCookie();
	} else {
		var date_split = part_of_split.split("=");
		var last = date_split[1];
		last=fixTheDate(last);
		document.write('<font color="#000000" font size="1" font face="Verdana, Helvetica">Your last visit was on:<BR></font><font color="#FF0000" font size="1" font face="Verdana, Helvetica">'+last);
		document.write('</font>');
		writeCookie();
	} // Close if (find_name==-1)
      }
} // Close function readCookie()


function writeCookie() {
     var today = new Date();
     var the_date = new Date("December 31, 2023");
     var the_cookie_date = the_date.toGMTString();
     var the_cookie = "nfti_date="+escape(today);
     var the_cookie = the_cookie + ";expires=" + the_cookie_date;
     document.cookie=the_cookie
}

function alertMessage(){
     document.write('<font color="#000000" font size="1" font face="Verdana, Helvetica">This is your first visit here.</font>')
}

function fixTheDate(date) {
     var split = date.split(" ");
     var fix_the_time = split[3].split(":")
     var hours = fix_the_time[0]
     if (hours>=12) {
	var ampm="PM"
     } else {
	var ampm="AM"
     }
     if (hours > 12) {
	hours = hours-12
     }
     var new_time = hours+":"+fix_the_time[1]+" "+ampm
     var new_date = split[0]+" "+split[1]+". "+split[2]+" at "+new_time+", "+split[5]
     return new_date;
}
//-->
</script>
[/color]
If you use a text editor to open the cookie that is set to the users machines, it will look something like this:

nfti_dateWed%20Aug%207%2012%3A35%3A45%20CDT%202002webdev/0252880076831079342369748411229507128*

Thats all there is to it. Play with the font styles, document.write statements and date/time formats to tweak the script to deliver the exact message you want.

[color red]WARNING[/color] changing the writeCookie() function might cause the script to generate errors. Don't change this one unless you know what you are doing.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top