Jump to content

Recommended Posts

<?php
  $counter_name = 'Counter.txt';

  if (!file_exists($counter_name)) {
    $f = fopen($counter_name, "w");
    fwrite($f,"0");
    fclose($f);
  }

  $f = fopen($counter_name,"r");
  $counterVal = fread($f, filesize($counter_name));
  fclose($f);

  $counterVal++;

  $f = fopen($counter_name, "w");
  fwrite($f, $counterVal);
  fclose($f);

  echo $counterVal;
?>

This is click counter... Counter.txt only number...

How to add a date?

Counter / Date

1. 2021-02-28 19:50:47

2. 2021-02-23 17:57:11

etc

 

Link to comment
https://forums.phpfreaks.com/topic/312230-click-counter/
Share on other sites

Append the current datetime each time instead of the count. The number of records will give you the count.

$counter_name = 'counter.txt';
file_put_contents($counter_name, date('Y-m-d H:i:s')."\n", FILE_APPEND);

$k = 0;
foreach (file($counter_name, FILE_IGNORE_NEW_LINES)  as $dt) {
    ++$k;
    echo "$k : $dt<br>";
}  

 

Edited by Barand
Link to comment
https://forums.phpfreaks.com/topic/312230-click-counter/#findComment-1584821
Share on other sites

If you're going to be prepending data to the file then you're also going to need to deal with file locks. In case two different PHP requests try to update the counter.

Obligatory "but you really shouldn't be using a file for this" comment here.

Link to comment
https://forums.phpfreaks.com/topic/312230-click-counter/#findComment-1584824
Share on other sites

Thanks

Code:
 

<?php
  $counter_name = 'counter.txt';
file_put_contents($counter_name, date('Y-m-d H:i:s')."\n", FILE_APPEND);

$k = 0;
foreach (file($counter_name, FILE_IGNORE_NEW_LINES)  as $dt) {
    ++$k;
    echo "$k : $dt<br>";
}  

  if (!file_exists($counter_name)) {
    $f = fopen($counter_name, "w");
    fwrite($f,"0");
    fclose($f);
  }

  $f = fopen($counter_name,"r");
  $counterVal = fread($f, filesize($counter_name));
  fclose($f);

  $counterVal++;

  $f = fopen($counter_name, "w");
  fwrite($f, $counterVal);
  fclose($f);

  echo $counterVal;
?>

Date work but counter no:

2021-03-02 14:56:08
2021-03-02 14:57:41
2021-03-02 14:57:42
2021-03-02 14:57:42
2021-03-02 14:57:45

Counter must have first 1) < counter

Time not correctly...

5) 2021-03-02 14:56:08 - new must be above like 14:57:45
4)2021-03-02 14:57:41
3) 2021-03-02 14:57:42
2) 2021-03-02 14:57:42
1) 2021-03-02 14:57:45

correctly is

5) 2021-03-02 14:57:45

4) 2021-03-02 14:57:42

3) 2021-03-02 14:57:42

2)2021-03-02 14:57:41

1) 2021-03-02 14:56:08

Very thanks

 

Link to comment
https://forums.phpfreaks.com/topic/312230-click-counter/#findComment-1584836
Share on other sites

If you want latest first, reverse the array

$counter_name = 'counter.txt';
file_put_contents($counter_name, date('Y-m-d H:i:s')."\n", FILE_APPEND);     // creates file if it doesn't exist
                                                                             // appends if it does exist

$k = 0;
$clicks = file($counter_name, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
$clicks = array_reverse($clicks);
foreach ($clicks as $dt) {
    ++$k;
    echo "$k : $dt<br>";
}

giving

1 : 2021-03-02 13:27:25
2 : 2021-03-02 13:26:23
3 : 2021-03-02 13:24:19
4 : 2021-03-01 18:28:04
5 : 2021-03-01 18:25:28
6 : 2021-03-01 18:25:23
7 : 2021-03-01 18:25:17

 

Link to comment
https://forums.phpfreaks.com/topic/312230-click-counter/#findComment-1584837
Share on other sites

<?php
$counter_name = 'counter.txt';
file_put_contents($counter_name, date('Y-m-d H:i:s')."\n", FILE_APPEND);     // creates file if it doesn't exist
                                                                             // appends if it does exist

$k = 0;
$clicks = file($counter_name, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
$clicks = array_reverse($clicks);
foreach ($clicks as $dt) {
    ++$k;
    echo "$k : $dt<br>";
}
?>
  <script>
      $("button.counter").on("click", function(e){
        e.preventDefault();
        $.ajax({
          url: 'counter.php',
          type: 'POST',
          success: function(counterVal){
        
          }
        });
      });
    </script>	

Dunno.. I got only date...

Link to comment
https://forums.phpfreaks.com/topic/312230-click-counter/#findComment-1584839
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.