Tuesday, May 21, 2013
This is my 2nd article about page or post view widget for blogger, or sometimes we call it counter widget, hit counter or even stats widget. Similar with its 1st article, in this 2nd article i'll tell you how to create page view widget for blogger, but ain't like the previous one that counts hits for every single page, here the widget will load unique and total hits of your website.

With this i give you an option to choose if you want to show total hits of your website, along with unique views or not, or you want to show only page hits of every single post. Of course, you can also show all of those if you'd like. Its scripts are not heavy anyway, each of scripts approximately is only 1Kb in size.

If you are not familiar with php and mysql, you'll have to read my previous post: Counter Widget Blogger. There you can learn steps of how to create a counter widget from start till the end, here i will get straight down without further explanation.

How To Create Total And Uniqne Page View Widget For Blogger

Of course you need to create a new table for this, while for database you can still use your existing database or you can create a new one if you'd like. Below is the parameter for your new table:

CREATE TABLE `tuviews` (
`ip` varchar(15) NOT NULL default '',
`visits` int(11) NOT NULL default '0'

Here is your php script:


$host = 'mysql.idhostinger.com';
$username = 'u2292_stat';
$password = '2345';
$db_name = 'u2292_stat';
$tbl_name = 'tuviews';
$sql_connect = mysql_connect("$host", "$username", "$password") or die("MySQL Connection Failed: " . mysql_error());;
$db_connect = mysql_select_db("$db_name") or die("Could not select DB: " . mysql_error());;

$fetch = mysql_query("SELECT ip FROM $tbl_name WHERE ip ='".$ip."'") or die(mysql_error());
if ( mysql_num_rows($fetch) == 0 )
mysql_query("INSERT INTO $tbl_name(ip,visits) VALUES('$ip','1')") or die(mysql_error());
else {
mysql_query("UPDATE $tbl_name SET visits=visits+1 WHERE ip = '$ip'") or die(mysql_error());

$count = mysql_query("SELECT * FROM $tbl_name") or die(mysql_error());
$unique = mysql_num_rows($count);
while ($vi = mysql_fetch_array($count)) {
$visits = $visits + $vi['visits'];

echo "document.write($unique);";
echo "document.write($visits);";

Here to copy


*The following are codes that call the script, you can decide to show only one or both of those:

echo "document.write($visits);"; --< This is total visits
echo "document.write($unique);"; --< This is unique visits

Well done! Your total views widget is ready to be called. Next, i have a php script to show you who's being online on your website, wait for that.


Post a Comment