?
Solved

PHP visitor duration

Posted on 2008-10-15
2
Medium Priority
?
499 Views
Last Modified: 2013-12-13
I'm trying to make a php script that indexes durations of page views on my site.

I've been doing some fiddling but I've hit a dead end, here's what I've come up so far:

1. every time the page is loaded, a random token is created and save as $_SESSION["token"] == $random;
2. a php file checks if the token is in the mysql database, if found, it updates the duration by 1. if not found, it creates a new entry and the cycle begins all over again

now, the only problem is that I need to refresh the site every second to be able to add +1 value to the duration, so I tried making an invisible iframe to do that and added a javascript reload timer, but even though the website doesn't fully refresh, you can still signs of something refreshing. then I tried making a while(0==0) { ... sleep(1); } loop inside the php script, but it somehow overflows the server and the page fails to load.

does anyone have an idea how to refresh the .php file embedded some way in the site without any visible activity?

maybe this could be done with ajax? the problem is that I don't know anything about it :/
0
Comment
Question by:CodilX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
AlexanderR earned 700 total points
ID: 22727357
Without knowing how you generate tokens or your DB structure, i'll just concentrate on the AJAX part.

Edit function send_duration so that it sends appropriate info.  In this example it just sends the time difference (or duration of the user looking at the page) as a GET parameter for timer.php.  Then you can pick it up at timer.php and load it into DB.  You can edit the url (second parameter of the xmlHttp.open function) to fit your need.  You can have <?php echo somevar;?> anywhere in there (remember, this is javascript so it does not really know about any of your php variables).
Adjust setTimeout value as you need.

I hope this is clear.
<html>
<head>
<script type="text/javascript">
var xmlHttp = createXmlHttpRequestObject();
var time_loaded = new Date();
 
function createXmlHttpRequestObject()
{
   var xmlHttp;
   try
   {
      xmlHttp = new XMLHttpRequest();
   }
   catch(e)
   {
      xmlHttp = false;
   }
   if(!xmlHttp)
   {
      alert("Error creating the xmlHttpRequest object");
   }
   else
   {
      return xmlHttp;
   }
}
 
function send_duration(){
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
   {
      var time_now = new Date();
      var time_difference = time_now - time_loaded;
      xmlHttp.open("GET", "timer.php?time="+time_difference, true);
      xmlHttp.send(null);
 
      setTimeout('send_duration()', 1000);
   }
   else
   {
      // if the connection is busy try again after one second
      setTimeout('send_duration()', 1000);
   }
}
 
 
</script>
</head>
<body onload="send_duration()">
THIS IS THE PAGE TO BE TIMED 
</body>
</html>

Open in new window

0
 

Author Comment

by:CodilX
ID: 22727521
thank you so much :) works like a dream
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question