you can use this simple script to add 100% accurate page views and unique visitors counter for your website. It's use php,mysql,JavaScript language and bootstrap,Raphael. js and Morris.js MySQL database connection
You should add your database information and add this script in all other 3 script
connect_error) {
die("Connection failed: " . $con->connect_error);}
?>
Create MySQL table
query($sql) === TRUE) {
echo "Table created successfully
";
} else {
echo "Error creating table: " . $con->error;
}
Create php script to get page views,unique visitors and update database table
You have to add this php script on every pages.
query($q);
if($result->num_rows > 0){
$rpv = $result->fetch_assoc();
if($rpv['cdate']==$date){
$pageview= $rpv['pageview']+1;
$q="UPDATE pv SET pageview='$pageview' WHERE cdate='$date'";
}else{
$q="INSERT INTO pv (pageview,cdate) VALUES('1','$date')";
}
}else{
$q="INSERT INTO pv (pageview,cdate) VALUES('1','$date')";
}
$con->query($q);
$ip=preg_replace('#[^0-9.]#','', getenv('REMOTE_ADDR'));
$q="SELECT visitor FROM visitor WHERE visitor='$ip'";
$result=$con->query($q);
if($result->num_rows==0){
$q='SELECT * FROM pv ORDER BY id DESC';
$result=$con->query($q);
if($result->num_rows > 0){
$rpv = $result->fetch_assoc();
if($rpv['cdate']==$date){
$uniquepageview= $rpv['uniquepageview']+1;
$q="UPDATE pv SET uniquepageview='$uniquepageview' WHERE cdate='$date'";
}else{
$q="INSERT INTO pv (uniquepageview,cdate) VALUES('1','$date')";
}
}else{
$q="INSERT INTO pv (uniquepageview,cdate) VALUES('1','$date')";
}
$con->query($q);
$q="INSERT INTO visitor (visitor) VALUE('$ip')" ;
$con->query($q);
}
?>
php script to show total page views and unique visitors with date
Graph styles
you may like simple hit cunter without database
Hello,
ReplyDeletei get an error on this script:
Create php script to get page views,unique visitors and update database table
You have to add this php script on every pages.
Print out this error:
Trying to get property 'num_rows' of non-object in C:\xampp\htdocs\PHP_scripts\Views-tracker\index.php on line 35
hello,
Deleteyou have an error in your SQL query. add echo $con->error; in line 34 to find error.