The simplest page view counter - PHP code example
By Sergey Skudaev
![]()
![]()
![]()
![]()
![]()
There are many PHP code examples of visitor counters or page view counters. I show you the simplest page counter PHP code example. You can include this script in every web page and it will display number of times each page was viewed by a user.
create a simple table in MySQL
CREATE TABLE `visitors_count` (
`countid` int(11) NOT NULL auto_increment,
`count` int(11) NOT NULL default '1'
`page` varchar(100) default NULL,
`remoteip` varchar(30) default NULL,
PRIMARY KEY (`countid`)
) ;
Enter 1 as default value for page count field. Page field should contain a web page name, for example, PHP file name. Enter all your page names in the page field. Use query:
"insert into visitors_count (countid, count, page) values(0, 1, 'index');
"insert into visitors_count (countid, count, page) values(0, 1, 'nextpage');
And so on.
If your web page is index.html file, then rename its extension to "php" and type at the bottom:
<
?$page="index";
include('visit_counter.php');
?>
The counter.php file contains the script that does the job
<?
$hostname = "localhost";
$dbuser = "auser";
$dbpassword = "apassword";
$dbname = "adatabase";
$db_link=mysql_connect($hostname, $dbuser, $dbpassword) or die("Unable toconnect to the server!");
mysql_select_db($dbname) or die("Unable to connect to the database");
$sql="select count from visitors_count where page='".$page."'";
$count=0;
$cresult=mysql_query($csql);
if(mysql_numrows($cresult))
{
$crow=mysql_fetch_row($cresult);
$count=$crow[0];
echo "Visitors:".$count."<br>";
}
$count=$count+1;
$usql="update visitors_count set count=".$count." where page='".$page."'";
if(!mysql_query($usql))
{
echo mysql_errno().":";<br>
echo mysql_error();<br>
}
?>
How it works?
1. You assign different value for $page variable on each your web page in the script, which you include in the page.
2. query will pull count value for this particular page.
3. Then you display count number
4. Then count variable is incremented by 1 and record updated.
Today, however, you do not need to have any page view counter. Sign up for google analytics and you will see not only how many visitors you have for any your web page, but also you will see where these visitors come from.
I was surprised by that!
Did you find information useful?
Send to your friend a link to this page
If you like this page click +1 button.
Please rate the tutorial
How to Build Your Own Web Site from Scratch [Kindle Edition] $2.99
Earn Money on Internet as an Affiliate [Kindle Edition] $0.99
| Comments | |
|---|---|
- Home
- page_view_counter
- Batch Files
- Java Properties
- Form Validation
- Display Image PHP
- Upload File PHP
- phpMyAdmin
- Environment Variables
- Delete Trojan horse
- PHP Code Examples
- Learn PHP
- Modeless Popup
- Read Screen Resolution
- PHP Class Example
- Page View Counter
- Display Any Table
- Read csv file
- PHP Array Functions
- Array of Fields
- GET mess
- Array of Check boxes
- PHP and MS Access
- User_Auth. Demo
- PHP App Example
- PHP + Oracle 1
- PHP + Oracle 2
- Image Collection
- Create Gallery
- Block Spam Post
- $_REQUEST,$_SESSION
- Headers Allredy Sent
- Virtual Server
- Apache:OS 10048
- Set Shopping Cart
- My Best CSS Layout
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn SQL
- Learn C++
- Learn Java
- Learn Visual Basic
- Web Site Tips
- Site map
- Registration
Web programming Tips