Register and show Time Spent from ip on website

Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
User avatar
JanMolendijk
Posts: 282
Location: Holland Rotterdam

Register and show Time Spent from ip on website

Dear admin, I`m searching for round 10 hours for a solution to track & store spending-time from a visitor on the website.

I found a Ajax script but it register only my own time-spending...
I have asked a question on stackover but that is not answered so I come back to you

Code: Select all

        <script type="text/javascript">
        $(function()
        {
            var start = null;
            $(window).load(function(event) {
                start = event.timeStamp;
            });
            $(window).unload(function(event) {
                var time = event.timeStamp - start;
                $.post('visit-timer/ajax-backend.php', {time: time});
            })
        });
        </script>
- php:

Code: Select all

<?php 
$time = intval($_POST['time']);
if (!file_exists('data1.php')) {
    file_put_contents('data1.php', $time . "---$ip---\r\n");
} else {
    file_put_contents('data1.php', $time . "---$ip---\r\n", FILE_APPEND);
}

?>

Admin Posts: 805
Hello,
I not have such a script, and, depending on what you want, it might require advanced ajax-php experience to make it.

1. Your script registers in data1.php the time spent on page for each ip. A better way is to change it to register data in json format.

Code: Select all

if(isset($_POST['time'])){
  $time = intval($_POST['time']);
  $ip = $_SERVER['REMOTE_ADDR'];
  $file ='data1.json';
  $f_data =[];

  if(!file_exists($file)){
    $f_data[$ip] = $time;
  } else {
    $f_data = json_decode(file_get_contents($file), true);
    if(isset($f_data[$ip])) $f_data[$ip] = $f_data[$ip]+ $time;
    else $f_data[$ip] = $time;
  }

  file_put_contents($file, json_encode($f_data));
}
2. To show all data registered in data1.json, you can run a php script like this:

Code: Select all

$file ='data1.json';
$f_data =[];

if(file_exists($file)) $f_data = json_decode(file_get_contents($file), true);

foreach($f_data as $k=>$v){
  echo '<br>'. $k.' = '.$v.' seconds';
}
- I didn't test it; if there are some minors errors, i think you can correct them.

JanMolendijk Posts: 282
Thanks for the support but with both examples it doesnt register all IP-Addresses faclt only my own

Admin Posts: 805
Maybe it depends on the server settings. I tested the script on a server and it works.
If you use your own local server, maybe there are some settings to make so the $_SERVER['REMOTE_ADDR'] to contain the visitor ip.

JanMolendijk Posts: 282
LOL I`m buisy with this problem for 40 hours now,
readed many articels but stil did not found any solution.

I use XAMPP server it logs all incomming visits but does not register on shutdown
I also have no clue what setting must be changed...

Hope you have any other tips what I could do ???

If their is something what I can register on shutdown from a page I would also be happy
because then I have a incomming register &nd a register by shutdown

Admin Posts: 805
Maybe it is not working with "onunload" to execute ajax request when the user closes the page.
Try the following code.

- JavaScript:

Code: Select all

<script>
var start = null;
$(window).on('load', function(event){
  start = event.timeStamp;
});

function pageClosed(event){
  $.ajax({
    type:'post',
    async: false,
    url:'logtime.php',
    data: {time:event.timeStamp - start},
    success: function(msg){
      console.log('ok');
    },
    error: function(error){
      alert(error);
    }
  });

  return true;
}

window.onbeforeunload = pageClosed;
</script>
- logtime.php:

Code: Select all

<?php
ignore_user_abort(true);

if(isset($_POST['time'])){
  $time = intval($_POST['time']);
  $ip = $_SERVER['REMOTE_ADDR'];
  $file ='data1.json';
  $f_data =[];

  if(!file_exists($file)){
    $f_data[$ip] = $time;
  } else {
    $f_data = json_decode(file_get_contents($file), true);
    if(isset($f_data[$ip])) $f_data[$ip] = $f_data[$ip]+ $time;
    else $f_data[$ip] = $time;
  }

  file_put_contents($file, json_encode($f_data));
}
- Be sure there are no JS errors in Browser console.
If it not work, try ask on other bigger forums.

Similar Topics