View Full Version : how to put a PHP hit counter

03-18-2009, 05:10 PM
HI friends,

I have two issues

I want to put a

1) website visit counter in my website
2) A download hits counter in my download page

I haven't used PHP to design my website, only HTML and javascript.

I think it's not possible to have these features using a client side script like javascript, and I think PHP would solve my problem.

but how can I run a PHP script in a HTML page, is it possible?
what would be the best way so that I will need to learn PHP?

03-18-2009, 05:22 PM
Well i would add a field in my MYSQL database called views, i would then edit my main page and add something like this :

$sql="SELECT * FROM views";



$views2= $views+1;

$sql2="UPDATE views set views='$views2' where views='$views'";

echo $views;


Hope this helps!

EDIT : sorry not INSERT, update :)

03-18-2009, 06:20 PM
If you're just starting out with PHP perhaps it would be easier to do counter using simple text file.

Save following code as counter.php

// Open file for reading
$file = fopen('count.txt', 'r');
// Read file
$data = fread($file, 1024);
// Close file

// Open file for writing
$file = fopen('count.txt', 'w');
// Write new visitor count increased by 1
fwrite($file, $data+1);
// Close file

// Show visitor count on the page
echo $data;

Also create new empty file called count.txt, it will hold your visitor count. Place both files to directory where you web page is located. Rename you web page extension from .html to .php and add this code where you like visitor count to appear:

<?php include('counter.php'); ?>