- Sep 14, 2002
- 11
- 0
- 0
Hi. I am working on a webstats scipt that should enter into a Mysql database how much time a user spends on a webpage.
Here is how my method goes:
Each page of my website contains a Javascript call to an image taht is actually a PHP script, like this:
Browser, sys, referrer, etc are Javascript variables sent on the query string to the php script.
The PHP script, counter.php, grabs these variables from the query string and puts them in a Mysql databse.
How can i get the time someone spends on a page using this method?
I was thinking of letting counter.php set $enter_time=time() to set the start time of the visit. Then i will need a variable $leave_time=time() when the user leaves the page (in whatever way). Then i can calculate the time spent on the page in seconds: $time_spent = $leave_time - $enter_time.
How do i set $leave_time?
I could do this by letting javascript create a popup when a user leaves the page. The popup will contain a php script that sets $leave_time, extracts $enter_time from the database, calculates $time_spent and writes it to the database. However this seems very unprofessional. Isn't there a better way?
Here is how my method goes:
Each page of my website contains a Javascript call to an image taht is actually a PHP script, like this:
statImg = new Image();
statImg.src = "counter.php?a='+browser+'&b='+sys+'&c='+referrer+'&d='+ip+'&e='+page+'";
Browser, sys, referrer, etc are Javascript variables sent on the query string to the php script.
The PHP script, counter.php, grabs these variables from the query string and puts them in a Mysql databse.
How can i get the time someone spends on a page using this method?
I was thinking of letting counter.php set $enter_time=time() to set the start time of the visit. Then i will need a variable $leave_time=time() when the user leaves the page (in whatever way). Then i can calculate the time spent on the page in seconds: $time_spent = $leave_time - $enter_time.
How do i set $leave_time?
I could do this by letting javascript create a popup when a user leaves the page. The popup will contain a php script that sets $leave_time, extracts $enter_time from the database, calculates $time_spent and writes it to the database. However this seems very unprofessional. Isn't there a better way?