PHP Counter script tutorial

Create your own a simple PHP counter script using a basic knowledge of php + mysql and keep your visitors number in database for analysis.


In this tutorial, we create 1 PHP file for testing this code.
1. counter.php

1. Create table "counter" in database "test".
2. Create file counter.php.

STEP1: Create table "counter"

CREATE TABLE `counter` (
`counter` int(9) NOT NULL default '0'

STEP2: Create file counter.php

############### Code


// Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server ");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";


// if have no counter value set counter = 1
$sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')";

echo "You 're visitors No. ";
echo $counter;

// count more value
$sql2="update $tbl_name set counter='$addcounter'";


Similar Post

Automatic refresh webpage / Set time to redirect

When you need your web page automatic refresh in 5 second or any second, use this meta tag. it's a simple code, put it between HEAD tag in your web page.

PHP User online tutorial

This tutorial show you php script that count how many users are active on your site.