Jump to content

Custom Image Counter For Your Site


Recommended Posts

Here's a custom image counter that can be used with multible pages. Requires the font I've provided or one of your own. The way I use it on my site is to just put it in it's own directory because it will create a seperate data (text file) file for every page you use the counter on. Once you upload the script and the font to your site it's just a matter of adding the counter image tag to your pages like below:

 

<img src="http://byrondallas.heliohost.org/php/counter/index.php?file=demo_page&back=yellow&color=red">

 

You change the page identifier and colors by appending it to the url:

 

file=demo_page = identifies the page your counting

back=yellow = the image color

color=red = the font color

 

Here's a Demo Page with the script:

http://byrondallas.heliohost.org/php/counter/demo.php

 

 

 

Link to comment
Share on other sites

Also if you wanted to get a few stats from your visitors you could add this to the script:

 

# variables for data
$agent = $_SERVER[HTTP_USER_AGENT];
$ip = $_SERVER[REMOTE_ADDR];
$host = gethostbyaddr($ip);
$page = $_SERVER[HTTP_REFERER];

# compile new entry
$time = date( "m/d/y G.i:s", time() );
$stats = "<b>Time:</b> $time<br><b>IP:</b> $ip<br><b>Host:</b> $host<br><b>Agent:</b> $agent<br><b>Viewed</b> <a href=\"$page\">$page</a>\n";

# write data to the file
file_put_contents("$file.stats.txt", $stats, FILE_APPEND);

 

Then read the data file to an html output like so:

 

Time: 04/24/11 11.08:10

IP: 207.7.92.114

Host: setcronjob.com

Agent: SetCronJob/1.0 (+http://www.setcronjob.com/)

Viewed http://your_site.heliohost.org/tracker.php

 

Link to comment
Share on other sites

Thanks for this wonderful script. It will be good if the counter is resetted every 24hours(for counting daily visitors).

 

The only way that I know to do it would be with a cron job. The cron job would delete the count text file EVERY 24 hours.

 

Or you could add this to your script. What it does is create a text file just for the purpose of checking how old it is. If the "time_check.txt" is older than 24 hours when somebody visits your page, it will delete the "time_check.txt" and the "$file.txt" causing the counter to start over. The problem with this is that it might get reset in the middle of the day depending on when your last visiter was.

 

$ckfile = "time_check.txt";
if (!file_exists($ckfile)) {
file_put_contents($ckfile, "nothing");
}
else
{

# check file age
$age = time()-filemtime($ckfile);

# how many days before file gets deleted
$dys = 1;
$expire = 60*60*24*$dys;

if ( $age > $expire )
{
unlink($ckfile);
unlink("$file.txt");
}
}

Link to comment
Share on other sites

You don't need any cron job. You could replace this part of the code:

#### If txt file exists get last count and add 1
$hits = file_get_contents("$file.txt");
$hits++;
file_put_contents("$file.txt", $hits);
}

with this:

#### If txt file exists get last count and add 1
if (date("Ymd", filemtime("$file.txt")) == date("Ymd"))
{
  $hits = file_get_contents("$file.txt");
  $hits++;
}
else
{ $hits = 1; }
file_put_contents("$file.txt", $hits);
}

 

And if you would like to store archive data for previous day for own statistic purposes:

#### If txt file exists get last count and add 1
if (date("Ymd", filemtime("$file.txt")) == date("Ymd"))
{
  $hits = file_get_contents("$file.txt");
  $hits++;
}
else
{
  rename("$file.txt", date("Ymd", filemtime("$file.txt")) . "$file.txt");
  $hits = 1;
}
file_put_contents("$file.txt", $hits);
}

 

 

Of course remember to set your desired time zone if it's different than default one set on the server for the counter being resetted at the right midnight, ie.:

putenv("TZ=America/New_York");

 

 

 

edit:

I didn't see edited by byron "Or you could add this to ...", yet, while writing my reply. What is worth to mention, though, that while byron's edited script with "$ckfile" will do the reset, but not necessarily at the midnight.

Link to comment
Share on other sites

I didn't see edited by byron "Or you could add this to ...", yet, while writing my reply. What is worth to mention, though, that while byron's edited script with "$ckfile" will do the reset, but not necessarily at the midnight.

 

Your solution was MUCH better than my long drawn out solution. Thank you! :)

 

Link to comment
Share on other sites

I should have mentioned also that you don't actually need a true type font. We can use the server fonts that come with ImageMagick. This tool will give you the font names and show you what they look like:

 

http://byrondallas.heliohost.org/php/tools...font-viewer.php

 

Just replace this line in the script:

 

$draw->setFont("trebuchet_italic.ttf");

 

With something like this (depending on the font name):

 

$draw->setFont("URW-Palladio-Roman");

 

That's the font I'm using now on my demo page:

 

http://byrondallas.heliohost.org/php/counter/demo.php

 

btw, this script only works for Stevie. Johnny doesn't have the Imagick Class.

 

Link to comment
Share on other sites

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.   Paste as plain text instead

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