Mr Chris Posted July 4, 2009 Share Posted July 4, 2009 Hello All, I'm trying to put a script on my site which will count the number of times an article has been viewed. Here's the tbl structure: CREATE TABLE `page_counter` ( `count_id` int(11) NOT NULL auto_increment, `count` int(20) NOT NULL default '1' `article_id` int(11) default NULL, `ip_address` varchar(30) default NULL, PRIMARY KEY (`count_id`) ) ; And the php <? $sql="select count from page_counter where article_id='".$article_id."'"; $count=0; $count_result=mysql_query($count_sql); if(mysql_num_rows($count_result)) { $count_row=mysql_fetch_row($count_result); $count=$count_row[0]; echo "This page has been accessed by:".$count." Visitors<br>"; } $count=$count+1; $update_sql="update page_counter set count=".$count." where article_id='".$article_id."'"; if(!mysql_query($update_sql)) { echo mysql_error(); } ?> Now this should work great but before I implement it, is there anyway I could make sure BOTS are not recorded in my counter, maybe by checking the user agent or is there a better way? Thanks Link to comment https://forums.phpfreaks.com/topic/164742-creating-a-page-count-in-php/ Share on other sites More sharing options...
Adika Posted July 4, 2009 Share Posted July 4, 2009 Your table is good, your php is good (it reads and writes in the database) but you also have an ip_address field in your database which you did not use in your php code provided in this site. (One note: $sql="select count from page_counter where article_id='".$article_id."'"; $count=0; $count_result=mysql_query($count_sql); Instead of this, you must put this for this script to work: $count_sql="select count from page_counter where article_id='".$article_id."'"; $count=0; $count_result=mysql_query($count_sql); ) If you are catching every visitors ip and before you update the database, you check if that user didn't already read that article, than you are safe from bots. Because the script that you showed us, are not doing that. What this script does is to raise the number every time you reload the page which is a big NO NO! Link to comment https://forums.phpfreaks.com/topic/164742-creating-a-page-count-in-php/#findComment-868808 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.