Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Set a Cookie and Retrieve Time
Old 08-29-2008, 12:17 PM Set a Cookie and Retrieve Time
Spud In Disguise's Avatar
Super Spam Talker

Posts: 808
Location: UK... where else?
Trades: 0
Hey guys, I need help creating something. I've thought about it and I think this would be the easiest solution to my problem, it's just I'm not totally sure on how to go about making it. Anyway, I need to time how long a user has spent on a specific page on my site. I recon this could be done by setting a cookie that records the time the page was loaded. There is then a link to another page and on that page will be the amount of time spent on the page. I guess this page will calculate the time difference between the time recorded in the cookie and the current time.

Can anyone help with this?
__________________

Please login or register to view this content. Registration is FREE
- Fansite

Please login or register to view this content. Registration is FREE
- Blog
Spud In Disguise is offline
Reply With Quote
View Public Profile Visit Spud In Disguise's homepage!
 
 
Register now for full access!
Old 08-30-2008, 12:28 PM Re: Set a Cookie and Retrieve Time
Extreme Talker

Posts: 177
Trades: 0
this is one solution I could see potentially being viable.

I guess I would use multiple cookies for this setup.

cookie set A
cookie[starttime] => start time of the current page
cookie[page] => page of the start time

cookie set B
cookie[endtime] => end time of the previous page


now when the page loads, you create the end time cookie (cookie set b). then you read the start time and the start time page cookie values(cookie set a), and you now have the time they spent on that page, and the page

then once you do this, you create the new cookie start time and cookie start page cookies(cookie set A) for the new page.

I'm pretty sure that will accomplish what you're looking for.
kbfirebreather is offline
Reply With Quote
View Public Profile
 
Old 08-31-2008, 07:08 AM Re: Set a Cookie and Retrieve Time
Spud In Disguise's Avatar
Super Spam Talker

Posts: 808
Location: UK... where else?
Trades: 0
Just to let you all know I now have a solution to this. I've included the code below in case any one else wants to use this system. Thanks for your help up to now though

Basically this times how long a user has a page loaded/open for and then writes that value to a cookie. The cookie is then retrieved and displayed on another page. I know there are a lot of potential problems with this, for example people opening another window or minimizing for a time, but for me that is perfect.


Page 1
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<script type="text/javascript">
function person_in(){enter=new Date();}
function person_out(){
var  exit=new Date();
var  time_dif=(exit.getTime()-enter.getTime())/1000;
  time_dif=Math.round(time_dif);
  alert("You've only been on this page for: "+time_dif+" seconds!!");
createCookie('myCookie', time_dif ,1*24*60*60*1000); // set for 1 day
}

function load(e) {
var enabled = Cookie.isEnabled();
if (!enabled)
alert('You must enable cookies.');
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
</script>
</head>

<body onLoad="person_in()" onUnload="person_out()">
<a href="page2.htm">link</a> 
</body>
</html>

Page 2
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<script type="text/javascript">
function load(e) {
var enabled = Cookie.isEnabled();
if (!enabled)
alert('You must enable cookies.');
}
document.write(readCookie('myCookie')); 

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
</script>
</head>

<body>
</body>
</html>
__________________

Please login or register to view this content. Registration is FREE
- Fansite

Please login or register to view this content. Registration is FREE
- Blog
Spud In Disguise is offline
Reply With Quote
View Public Profile Visit Spud In Disguise's homepage!
 
Reply     « Reply to Set a Cookie and Retrieve Time
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31138 seconds with 12 queries