Count Visitors of a page

Count Users

We are here to discuss how to count number of visitors in a web page using session.

Please create a web page index.php

We have first started the session.

Before starting further you will need to create a database counter and then create a table in it.

Please follow the codes to create a table

CREATE TABLE counts (
    cid int NOT NULL AUTO_INCREMENT,
    
    pagecounter int,
    PRIMARY KEY (cid)
);

Now before we proceed further, we insert the first value . Please run the following codes

INSERT INTO `counts` (`cid`, `pagecounter`) VALUES ('1', '1');

Now table has been feeded with the first value.

Now in index.php please copy the following codes.

Index.php

<?php
session_start();
$conc=mysqli_connect('localhost','root','','counter') or die(mysql_error());
$q=mysqli_query($conc,"SELECT * FROM counts ");
    
         while ($row = mysqli_fetch_array($q)) {
         $c = $row['pagecounter'];
         $c = $c +1;
         }
if(!isset($_SESSION['counter']))
    {
 
$_SESSION["counter"] = "set";

         $uid = 1;
 $sql23 = "UPDATE counts SET pagecounter = '$c' WHERE cid = '$uid'";
//echo $sql23;
            $result=mysqli_query($conc,$sql23);

}
        echo $c;
?>

 

The above page will display the number of counters using session value.

 

 

Share the post


Leave Comment