IP Retrival & hit counter with PHP/GD Library image editing

IP Retrival & hit counter with PHP/GD Library image editing

Synopsis:

This tutorial shows how to make a PHP image which generates IP Address of the PC viewing it and act as a HIT counter .

The Article:

This is a really simple free php hit counter script that uses the GD library to generate a picture that displays the users IP address and the amount of views the picture / page has had.This can be used as a hit counter for your siteor as a cool forum signature.Your host will need php and the GD library installed to use this Free Hit Counter.

1. First save a blank .txt file named counter.txt
2. then open notepad (or any PHP-editor u use) and paste this code :

&ltl?php

$TextFile = “counter.txt”;
$Count = trim(file_get_contents($TextFile));
$FP = fopen($TextFile, “r”);
$Count=fgets($FP, 4096);
fclose ($FP);
settype($Count, “integer”);
$Count++;
if ($FP = fopen ($TextFile, “w”)){
fwrite ($FP, $Count);
fclose ($FP);

}

$image = “counterpic.png”;
$im = imagecreatefrompng($image);
$red = ImageColorAllocate ($im, 255, 0, 0);
$blue = ImageColorAllocate ($im, 0, 0, 255);
$hit = “$Count”;
$ip = $_SERVER[”REMOTE_ADDR”];

ImageString($im, 2, 18, 1, “www.yoursitename.com”, $blue);
ImageString($im, 2, 1, 19, ” Your ip: $ip”, $red);
ImageString($im, 2, 1, 30, ” Page visited $hit times “, $red);
header(”Content-Type: image/png”);
Imagepng($im,'’,100);
ImageDestroy ($im);

?>

3. Save it as index.php or anything u like just with .php extention

4.Now save the 3 files index.php , counter.txt & a background image some thing like a blue rectangle in the name counterpic.png in a folder and upload it through FTP to any server supporting php

sample image counterpic.png

5. Grant counter.txt CMOD 777 ( right click in FTP > properties > all previlages)

6. Then call index.php in your browser . that`s it.

TIP:

1. You can link to the index.php with the normal image tags.

2. If you upload these files to a folder called pic.gif you will be able to use it on forums by using the url yoursite.com/pic.gif
and it will run the index.php and output the image, Useful if the site or forum don’t allow php images or html code.

3. You can enter any no. in counter.txt the image will show that much hits !!!! LOL

4. This is the final image

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.