Simple visitors counter in php?

Simple visitors counter in php?

This article talk about how to create visitors counter for you web pages using php & mysql without using any external library or plugin. This technique will not let you to count unique visitors. 

Step 01 

As first step you can create new table or added new column to your database. After that we have to connect your php page to update & retrieves data from your database using mysql. Code as follows:


    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "dbname";
    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } ?>


Step 02 

As second step what we have to do is use one  query to retrieve visitors count currently on database. code as follows:

$sql = "SELECT (COLUMN_NAME) FROM (TABLE)"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $visitors_count = $row["COLUMN_NAME"]; } } else { echo "no results"; }


Step 03

Once we retrieve data to $visitors_count variable what we have to do is all time page load we have to count +1 for this variable. After that we have to use another query to update current visitor + already counted visitors. Code as follows: 

$visits = ++$visits;
$sql = "UPDATE (TABEL_NAME) SET (COLUMN_NAME) =$visitors_count";

Step 04

Now we are done with our visitors counter using php & mysql. Enjoy your visitors counter with your web site. If you need to display current visitors count what you have to do it echo your variable ($visitors_count) where you want to display.

Visitors : 16