Davet Posted March 1, 2021 Share Posted March 1, 2021 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/312230-click-counter/ Share on other sites More sharing options...
Barand Posted March 1, 2021 Share Posted March 1, 2021 (edited) 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 March 1, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/312230-click-counter/#findComment-1584821 Share on other sites More sharing options...
requinix Posted March 1, 2021 Share Posted March 1, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312230-click-counter/#findComment-1584824 Share on other sites More sharing options...
Davet Posted March 2, 2021 Author Share Posted March 2, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/312230-click-counter/#findComment-1584836 Share on other sites More sharing options...
Barand Posted March 2, 2021 Share Posted March 2, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/312230-click-counter/#findComment-1584837 Share on other sites More sharing options...
Davet Posted March 2, 2021 Author Share Posted March 2, 2021 <?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... Quote Link to comment https://forums.phpfreaks.com/topic/312230-click-counter/#findComment-1584839 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.