How To Make PHP Page Counter
Make counter using session PHP
Counter example
<?php session_start(); $total=$_SESSION['count']=$_SESSION['count']+1; Echo "you are visited this site ". $total. " times"; ?>
- Example
- Run
Explanation
In this example we used session function to make the counter firstly start a session using session_start function. Then make a variable name $total for storing the total value of visiting page and add 1 in $_SESSION['count']. It will 1 every time when page load and print the $total variable and display the no on visited page.
Now you can check your counter by doing your page refresh.
Making counter using file handling
Counter example
<?php $file = file_get_contents("count.txt"); $total=$file; $newtotal=$total+1; $newfile=fopen("count.txt","w"); fwrite($newfile,$newtotal); echo $newtotal; ?>
- Example
- Run
Explanation
In this example we are going to make simple PHP counter using file handling.
Now you can check your counter by doing your page refresh.