Simple Visitor Counter Using php

Last updated on January 30th, 2022 at 07:42 pm

Click to rate this tutorial!
[Total: 47 Average: 4.3]

This tutorial require 1 PHP file and 1 table of mySQL database.

1. counter.php
2. Database “mypage” and table “counter” with 1 fields: visitor(Int, 11). You need to insert a first one record with “0”.

You might also be interested in

1] Hit Counter using PHP and Text File (With Demo / Any PHP Version)

2] Image hit counter using PHP/MySQL (With Demo / PHP 5 and Above)

Counter.php
Source Code (Only compatible with PHP 5 or lower version)

<?php
// For using session variables put this function on the top.
session_start();
 
// Connect database
mysql_connect("localhost","","");
mysql_select_db("mypage");
 
/* Check for session variable "visitor".
If not exist, update database and create this session by Session ID.*/
if(!session_is_registered("visitor")){
$visitor=session_id();
session_register("visitor");
 
// Update value in column "all_visitor" by +1.
mysql_query("update counter set visitors=visitors+'1'");
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Counter</title>
</head>
<body>
Counter :
<?php
// Select data from easy_counter and put them into $result.
$result=mysql_query("select * from counter");
$row=mysql_fetch_assoc($result);
 
// Output all_visitor record.
echo $row['visitors'];
 
// Close database connection.
mysql_close();
?>
</body>
</html>

Click to rate this tutorial!
[Total: 47 Average: 4.3]

2 replies on “Simple Visitor Counter Using php”

omg, you tell me to create a table ‘counter’ with one column ‘visitor’
but your query set visitorS=visitorS+’1′ :\
thanks for your help though

Leave a Reply

Your email address will not be published.