PHP Tutorial : Simple views counter script

Hi,

In this php tutorial you will learn the simple way to build a script which counts how many times a page is viewed. For this, we can use a mysql database table and a simple php script.

<?php
$pagename = $_SERVER['PHP_SELF'];
$sql = "select * from mysqltable where page = '$pagename'";
$rs = mysql_query($sql);

if($rs) {
$row=@mysql_fetch_object($rs);
$counter = $row->counterfield;
//update the stats, if they exits

$counter = "".($counter+1)."";
$qry = mysql_query("updated tablename set counterfield = '$counter' where id = '$row->id'");
}else{

print mysql_error();
}
?>
This entry was posted in Usefull PHP and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>