Jump to content

Need to save the amount of visits per day and clear counter.


co.ador

Recommended Posts

<?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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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}";
?>

Link to comment
Share on other sites

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 :)

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
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.