1

OK so I've adapted some code I found to hopefully record a click event and display it live in my wordpress site, but it isn't working. here is the code I've written. This is my fist attempt at writing code in general. Can anyone see where i'm going wrong?

doing_totalcount.php

    <?php
if (onclick"<a href="#" class="menu-link">EDGE</a>($counters_folder . md5($file) . '_counter.txt')) {
          $fp = fopen($counters_folder . md5($file) . '_counter.txt', "r");
          $count = fread($fp, 1024);
          fclose($fp);
          $fp = fopen($counters_folder . md5($file) . '_counter.txt', "w");
          fwrite($fp, $count + 1);
          fclose($fp);
        } else {
          $fp = fopen($counters_folder . md5($file) . '_counter.txt', "w+");
          fwrite($fp, 1);
          fclose($fp);
        }
?>

count_totalcount.php

    <?php
  function get_totalcount_count($file = null) {
    $counters = $_SERVER['DOCUMENT_ROOT'] . '/files/counter/';
    if ($file == null) return 0;
    $count = 0;
    if (file_exists($counters . md5($file) . '_counter.txt')) {
      $fp = fopen($counters . md5($file) . '_counter.txt', "r");
      $count = fread($fp, 1024);
      fclose($fp);
    } else {
      $fp = fopen($counters . md5($file) . '_counter.txt', "w+");
      fwrite($fp, $count);
      fclose($fp);
    }
    return $count;
  }
?>

call_totlcount.php

    <?php
  include($_SERVER["DOCUMENT_ROOT"] . "/php/count_totalcount.php");
  $item['lifetime'] = get_totalcount_count;
  echo json_encode($item);
?>

static_totalcount.php

    <?php
  include($_SERVER["DOCUMENT_ROOT"] . "/php/count_totalcount.php");
  $item['lifetime'] = get_totalcount_count;
 
?>

totalcount.js

$(document).ready(function() {
  $.ajaxSetup({cache: false});
  getStatus();
});
function getStatus() {
  $.getJSON('php/call_totalcount.php', function(data) {
    $('#lifetime').html(data.lifetime);
  });
  setTimeout("getStatus()",1000);
}

index.php

    <?php include($_SERVER["DOCUMENT_ROOT"] . "/php/static_totalcount.php"); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Landing page</title>
<script type="text/javascript" src="jsc/jquery.min.js"></script>
<script type="text/javascript" src="jsc/totalcount.js"></script>
</head>
<body>
You have clicked the button <span id="lifetime"><?php echo $item['lifetime']; ?></span> Times<br />
</body>
</html>

This is the link to the original question that i took the code from, so you can see what I changed so it was relevant to my needs

PHP making a download counter without leaving the current page

  • Give us a clue...what debugging have you done? Any error messages (either client side or server side), any unexpected behaviour compared to what you expected? You need to be more specific, especially as you've posted quite a bit of code. We can't debug it for you, but we can help you interpret the results of that work – ADyson May 12 '21 at 06:46
  • P.s. One observation: `get_totalcount_count;` isn't how you call a function...this is likely to be producing an error or warning in PHP. Also why do you (try to) call it twice? – ADyson May 12 '21 at 06:48
  • 1
    And why does `call_totalcount.php` try to include itself? That will likely cause an infinite loop. And it's unclear what the purpose of `doing_totalcount.php` is, you don't seem to use it anywhere and the `if` statement in it doesn't make a lot of sense (and isn't valid PHP syntax anyway) and seems to repeat a lot of what's in count_totalcount.php – ADyson May 12 '21 at 06:51

0 Answers0