co.ador Posted December 27, 2009 Share Posted December 27, 2009 <?php <?php $count_my_page = ("hitcounter.txt"); $hits = file($count_my_page); $hits[0] ++; $fp = fopen($count_my_page , "w"); fputs($fp , "$hits[0]"); fclose($fp); echo $hits[0] . ' Visits today.'; ?> ?> So far the script below will count the amount of visits to the page.. The script above don't take into consideration the date when they have visit or anything like that. Now I want to open the file expose the content but then at the end of the day let's say 12:00am to clear up the hitcounter.txt but before clearing it up to save the amount of count and the date it was collected. Can anybody help to figure out what functions will be needed for that? Quote Link to comment Share on other sites More sharing options...
Zyx Posted December 27, 2009 Share Posted December 27, 2009 Save the Unix timestamp together with the date of the last refreshment (time() function). Every time you want to increase the counter, you calculate the beginning of the current day with mktime() and compare it with the time stored in the file. If the time is lower, it means that we have the first entry in the next day and we must reset the counter, otherwise you increment the existing value. Quote Link to comment Share on other sites More sharing options...
laffin Posted December 27, 2009 Share Posted December 27, 2009 U should really consider using a db for hitcounters. but if its not a site that gets a lot of hits in first place, it shouldnt matter <?php $hitcounter='hitcounter.txt'; $ftime=intval(date('Ymd',filemtime($hitcounter))); $ctime=intval(date('Ymd')); $counter=intval(file_get_contents($hitcounter)); if($ctime>$ftime) { $fh=fopen('hitcounter.log','at'); fputs($fh,"{$ftime} - {$counter}\n"); fclose($fh); $counter=1; } else { $counter++; } file_put_contents($hitcounter,$counter); echo "Visits Today: {$counter}"; ?> Quote Link to comment Share on other sites More sharing options...
co.ador Posted December 27, 2009 Author Share Posted December 27, 2009 don't you mind putting some comments please. I would like to know how the scripts is working. Thank you for the idea Quote Link to comment Share on other sites More sharing options...
laffin Posted December 27, 2009 Share Posted December 27, 2009 Its would be much better preferred, if u used http://php.net/manual/en/ to look up functions you dont know offhand. Just because it provides so much more detail about the functions I used in the script. because general comments, doesnt mean you will understand the code any better. The script isnt that hard to figure out, once you know what functions it uses, and how it uses them Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 27, 2009 Share Posted December 27, 2009 You should really use a CRON job for this, if you're not wanting to use a database. A database is the simplest way to go, Create a table based on the day (date('D')) and then the code will be truly dynamic, pulling the counter of the next day as the next day is created. A CRON approach will run 'clearcounter.php' for example, and will reset it for you every end of the day, a much more simple approach on if you, say, wanted to keep it simple. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted December 27, 2009 Share Posted December 27, 2009 I would go by year and day of year. Have a folder for 2010, check to see if file.day.of.year exists, if it exists its todays file and count/increment to it. If it does not exist its the first visit for today so writing to it will create it. Easy and not much to keep track of. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
co.ador Posted December 27, 2009 Author Share Posted December 27, 2009 Guys I am going to study the functions first then I will go by all the steps you have given me thanks. I was studying intval function and I was wondering how will date('Ymd', and filemtime($hitcounter))); would be affected by the function intval? let's say date = 1999-12-04, that was the return date where hitcounter was last seen or modified. But then what's the function of intval, cause we have three values. From what I saw on the php manual is that intval has several meanings. One is to round, to make a number whole, What's going on in this case? Quote Link to comment Share on other sites More sharing options...
laffin Posted December 27, 2009 Share Posted December 27, 2009 intval has one meaning, makes a numeric into a integer. wheather it be a string/real number. the date function returns date information according to a format string Y - 4 digit year m - 2 digit month d - 2 digit day filemtime - returns a integer, representing the date, of the file last modified time so you add all them together, get files modified date, convert to a string in Ymd format (notice no - or /, which is for the next step), converting the string into an integer. Quote Link to comment 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.