User Tools

Site Tools


linux:contadorphp

Making a counter in PHP

If you search “free counter” in internet, you will probably find thousands of supposed websites that offer “free counters”. The reality is that those free counters doesn't offer any free counter, and nobody does: advertisments, sms you have to pay… always there is a point they will have to request a fee for the service: to put an starting number (instead of zero), to “start” the service…

This little code is a counter that you can add to a website that supports running php and can be added into your website, and is as clean and easy to use as the professional ones. Just add the following code into your website:

<img src="http://www.example.com/counter.php?id=website1">

And that's all!!! Moreover this code supports to add –via slight changes in the code– support to many websites.

The source code of the file counter.php is as follows:

<?php
 
import_request_variables('G', 'p_');
 
//set the count file
if( $p_id = "website1" )
{
  $counterfile = "contador.dat";
}
 
//check the file is readable
if(!($fp = fopen($counterfile,"r"))) die ("Contador roto!!!");
 
//grab the value and increase by 1
$count = (int) fread($fp, 20);
fclose($fp);
$count++;
 
//show the result, write the new value and close the file
$im = imagecreate( 60, 20);
 
 
$bg = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
 
imagestring( $im, 5, 3, 2, $count, $black );
 
header('Content-type: image/jpg');
 
imagejpeg($im);
 
$fp = fopen($counterfile, "w");
fwrite($fp , $count);
fclose($fp);
 
?>

And don't forget to create a file called “contador.dat” that have to be writable by the user who runs apache –if you don't know what I am speaking of, just run chmod 666 contador.dat and that's all–.

You can check out this website: http://www.joseluisluna.com in order to see what it looks like.

And if you want just a free counter service, please ask me and I will create another Id for you!!!

Raul Luna Rodriguez 2011/03/29 13:30

linux/contadorphp.txt · Last modified: 2014/12/24 10:42 (external edit)