Generally, when you develop a web-based applications, then you want to know the visitor statistics, such as the number of visitors hits on time basis, as well as the operating systems and the browsers they are using when visiting your web applications. This is very useful to monitor the statistical data of your web visitors in detail. Unfortunately, up to PHPMaker 9.0.2, there is no Visitors Statistics feature available.
This following customization will add the Visitors Statistics in your web applications that generated by PHPMaker 9. You will be able to know the number of visitor hits per hour, day, month, and year. In addition, you will be able to know the information about the date and time of first visit and last visit of your visitors based on their IP Address. Also, you will be able to know the operating systems and the browsers they are using while visiting your web applications.
You can enable or disable this feature by simply adjusting the related constant value in your configuration (ewcfg9.php) file. This customization includes the creating of six tables in order to store the visitor statistic records. And ouw, as always, we will not modify any generated web files, but only customizing PHPMaker template files.
Updated on July 22, 2012: This customization has been implemented in PHPMaker version 9.0.3, it matches to each other, and as a result, it works properly.
Updated on July 13, 2013: This customization has been handled by MasinoVisitorStatistics extension. You don’t need to implement the customization below if you use my extensions above together with the original PHPMaker Template and Extensions.
[hidepost]
-
First of all, generate the following script in order to create 6 tables and populate the related records into stats_counter table in your database. Please note that for simplicity, I am using MySQL script. You can convert it by yourself to the other database(s) from MySQL after the tables are generated.
The tables are:
- stats_counter
- stats_counterlog
- stats_date
- stats_hour
- stats_month
- stats_yearSET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `stats_counter` -- ---------------------------- DROP TABLE IF EXISTS `stats_counter`; CREATE TABLE `stats_counter` ( `Type` varchar(30) NOT NULL, `Variable` varchar(30) NOT NULL, `Counter` int(11) NOT NULL, PRIMARY KEY (`Type`,`Variable`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- ---------------------------- -- Records of stats_counter -- ---------------------------- INSERT INTO `stats_counter` VALUES ('browser', 'Bot', '0'); INSERT INTO `stats_counter` VALUES ('browser', 'FireFox', '0'); INSERT INTO `stats_counter` VALUES ('browser', 'Konqueror', '0'); INSERT INTO `stats_counter` VALUES ('browser', 'Lynx', '0'); INSERT INTO `stats_counter` VALUES ('browser', 'MSIE', '0'); INSERT INTO `stats_counter` VALUES ('browser', 'Netscape', '0'); INSERT INTO `stats_counter` VALUES ('browser', 'Opera', '0'); INSERT INTO `stats_counter` VALUES ('browser', 'Other', '0'); INSERT INTO `stats_counter` VALUES ('browser', 'WebTV', '0'); INSERT INTO `stats_counter` VALUES ('os', 'AIX', '0'); INSERT INTO `stats_counter` VALUES ('os', 'BeOS', '0'); INSERT INTO `stats_counter` VALUES ('os', 'FreeBSD', '0'); INSERT INTO `stats_counter` VALUES ('os', 'IRIX', '0'); INSERT INTO `stats_counter` VALUES ('os', 'Linux', '0'); INSERT INTO `stats_counter` VALUES ('os', 'Mac', '0'); INSERT INTO `stats_counter` VALUES ('os', 'OS/2', '0'); INSERT INTO `stats_counter` VALUES ('os', 'Other', '0'); INSERT INTO `stats_counter` VALUES ('os', 'SunOS', '0'); INSERT INTO `stats_counter` VALUES ('os', 'Windows', '0'); INSERT INTO `stats_counter` VALUES ('total', 'hits', '0'); -- ---------------------------- -- Table structure for `stats_counterlog` -- ---------------------------- DROP TABLE IF EXISTS `stats_counterlog`; CREATE TABLE `stats_counterlog` ( `IP_Address` varchar(50) NOT NULL, `Hostname` varchar(50) DEFAULT NULL, `First_Visit` datetime NOT NULL, `Last_Visit` datetime NOT NULL, `Counter` int(11) NOT NULL, PRIMARY KEY (`IP_Address`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- ---------------------------- -- Records of stats_counterlog -- ---------------------------- -- ---------------------------- -- Table structure for `stats_date` -- ---------------------------- DROP TABLE IF EXISTS `stats_date`; CREATE TABLE `stats_date` ( `Year` smallint(4) NOT NULL, `Month` tinyint(2) NOT NULL, `Date` tinyint(2) NOT NULL, `Hits` bigint(11) NOT NULL, PRIMARY KEY (`Year`,`Month`,`Date`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- ---------------------------- -- Records of stats_date -- ---------------------------- -- ---------------------------- -- Table structure for `stats_hour` -- ---------------------------- DROP TABLE IF EXISTS `stats_hour`; CREATE TABLE `stats_hour` ( `Year` smallint(4) NOT NULL, `Month` tinyint(2) NOT NULL, `Date` tinyint(2) NOT NULL, `Hour` tinyint(2) NOT NULL, `Hits` int(11) NOT NULL, PRIMARY KEY (`Year`,`Month`,`Date`,`Hour`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- ---------------------------- -- Records of stats_hour -- ---------------------------- -- ---------------------------- -- Table structure for `stats_month` -- ---------------------------- DROP TABLE IF EXISTS `stats_month`; CREATE TABLE `stats_month` ( `Year` smallint(4) NOT NULL, `Month` tinyint(2) NOT NULL, `Hits` bigint(20) NOT NULL, PRIMARY KEY (`Year`,`Month`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- ---------------------------- -- Records of stats_month -- ---------------------------- -- ---------------------------- -- Table structure for `stats_year` -- ---------------------------- DROP TABLE IF EXISTS `stats_year`; CREATE TABLE `stats_year` ( `Year` smallint(4) NOT NULL, `Hits` bigint(20) NOT NULL, PRIMARY KEY (`Year`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- ---------------------------- -- Records of stats_year -- ---------------------------- -
The next step, open your \Script\ewcfg.php file, and find this code:
// General
before that line, please insert the following code:
// Begin of modification Visitors Statistics, by Masino Sinaga, June 14, 2012 define("MS_LOG_VISITORS_STATISTICS", TRUE, TRUE); // End of modification Visitors Statistics, by Masino Sinaga, June 14, 2012 -
Open your PHPMaker project (.pmp) file using PHPMaker. Go to Code (Server Events, Client Scripts and Custom Templates) tab, and then expand the following section: Server Events -> Global -> All Pages -> Page_Loading. Insert the following code inside the Page_Loading function; just before the last curly braces in that function:
// Begin of modification Visitors Statistics, by Masino Sinaga, June 14, 2012 if (MS_LOG_VISITORS_STATISTICS) { include "counter.php"; } // End of modification Visitors Statistics, by Masino Sinaga, June 14, 2012 -
Open your \Script\control.xml file, and find this code (you might get the different code with the following, so adjust it with yours):
<!-- Js --> <control id="ew.js" type="other" ofolderid="_js" ifiles="ewp.js" ofile="ewp9" oext="js"> <session type="key" value="jscommon" /> </control> <control id="jsrender.js" type="copy" ofolderid="_js" ifiles="jsrender.js" /> <control id="sizzle-min.js" type="copy" ofolderid="_js" ifiles="sizzle-min.js" cond="function/LocaljQueryFiles/Boolean/EQ/False" /> <control id="jquery" type="copy" ofolder="jquery172" ifolder="jquery172" cond="function/LocaljQueryFiles/Boolean/EQ/True" /> <control id="datenumber-min.js" type="copy" ofolderid="_js" ifiles="datenumber-min.js" />
after the last line of that code, please insert the following code:
<control id="counter.php" type="copy" remark="Copy counter.php" ifiles="counter.php" />
-
Create a new file and save it as counter.php, and put it under your \Script sub-directory of your PHPMaker template. Afterwards, copy and paste the following code into that file:
<?php $user_ip = GetUserIPAddress(); // getenv("REMOTE_ADDR"); // <-- old way, don't use it!!! $user_host = gethostname(); // <-- PHP >= 5.3.0 // php_uname('n'); // <-- PHP 4 >= 4.0.2, PHP 5 // getenv("REMOTE_HOST"); // <-- PHP < 4.0.2 if ( IsLogEmpty($user_ip) == TRUE ) { AddLog($user_ip, $user_host); UpdateCounterLog($user_ip, $user_host); } else { UpdateCounterLog($user_ip, $user_host); } function GetUserIPAddress() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) // check ip address from share internet { $useripaddress = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) // to check if ip address is passed from proxy { $useripaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $useripaddress = $_SERVER['REMOTE_ADDR']; } return $useripaddress; } function IsLogEmpty ( $useripaddress ) { global $conn; $query = "SELECT Counter FROM stats_counterlog WHERE IP_Address = '".$useripaddress."'"; $rs = $conn->Execute($query); $lognum = $rs->RecordCount(); if ($lognum == "0") { return TRUE; } if ($lognum != "0") { return FALSE; } } function AddLog ( $useripaddress, $hostname ) { global $conn; $today = date("Y-m-d H:i:s"); $query = "INSERT INTO stats_counterlog VALUES ('".$useripaddress."', '".$hostname."', '".$today."', '".$today."', 1)"; // first time ever! :) $conn->Execute($query); } function UpdateCounterLog ( $useripaddress, $hostname ) { global $conn; $today = date("Y-m-d H:i:s"); $z = GetCounterLog($useripaddress); $z++; $query = "UPDATE stats_counterlog SET Counter = ".$z.", Last_Visit = '".$today."', Hostname='".$hostname."' WHERE IP_Address LIKE '".$useripaddress."'"; $conn->Execute($query); } function GetCounterLog ( $useripaddress ) { global $conn; $query = "SELECT Counter FROM stats_counterlog WHERE IP_Address LIKE '".$useripaddress."'"; $rscounter = $conn->Execute($query); $counter_value = $rscounter->fields("Counter"); return $counter_value; } /* Get the Browser data */ if ((@ereg("Nav", $_SERVER["HTTP_USER_AGENT"])) || (@ereg("Gold", $_SERVER["HTTP_USER_AGENT"])) || (@ereg("X11", $_SERVER["HTTP_USER_AGENT"])) || (@ereg("Mozilla", $_SERVER["HTTP_USER_AGENT"])) || (@ereg("Netscape", $_SERVER["HTTP_USER_AGENT"])) AND (!@ereg("MSIE", $_SERVER["HTTP_USER_AGENT"])) AND (!@ereg("Konqueror", $_SERVER["HTTP_USER_AGENT"])) AND (!@ereg("Yahoo", $_SERVER["HTTP_USER_AGENT"])) AND (!@ereg("Firefox", $_SERVER["HTTP_USER_AGENT"]))) $browser = "Netscape"; elseif(@ereg("Firefox", $_SERVER["HTTP_USER_AGENT"])) $browser = "FireFox"; elseif(@ereg("MSIE", $_SERVER["HTTP_USER_AGENT"])) $browser = "MSIE"; elseif(@ereg("Lynx", $_SERVER["HTTP_USER_AGENT"])) $browser = "Lynx"; elseif(@ereg("Opera", $_SERVER["HTTP_USER_AGENT"])) $browser = "Opera"; elseif(@ereg("WebTV", $_SERVER["HTTP_USER_AGENT"])) $browser = "WebTV"; elseif(@ereg("Konqueror", $_SERVER["HTTP_USER_AGENT"])) $browser = "Konqueror"; elseif((eregi("bot", $_SERVER["HTTP_USER_AGENT"])) || (@ereg("Google", $_SERVER["HTTP_USER_AGENT"])) || (@ereg("Slurp", $_SERVER["HTTP_USER_AGENT"])) || (@ereg("Scooter", $_SERVER["HTTP_USER_AGENT"])) || (eregi("Spider", $_SERVER["HTTP_USER_AGENT"])) || (eregi("Infoseek", $_SERVER["HTTP_USER_AGENT"]))) $browser = "Bot"; else $browser = "Other"; /* Get the Operating System data */ if(@ereg("Win", $_SERVER["HTTP_USER_AGENT"])) $os = "Windows"; elseif((@ereg("Mac", $_SERVER["HTTP_USER_AGENT"])) || (@ereg("PPC", $_SERVER["HTTP_USER_AGENT"]))) $os = "Mac"; elseif(@ereg("Linux", $_SERVER["HTTP_USER_AGENT"])) $os = "Linux"; elseif(@ereg("FreeBSD", $_SERVER["HTTP_USER_AGENT"])) $os = "FreeBSD"; elseif(@ereg("SunOS", $_SERVER["HTTP_USER_AGENT"])) $os = "SunOS"; elseif(@ereg("IRIX", $_SERVER["HTTP_USER_AGENT"])) $os = "IRIX"; elseif(@ereg("BeOS", $_SERVER["HTTP_USER_AGENT"])) $os = "BeOS"; elseif(@ereg("OS/2", $_SERVER["HTTP_USER_AGENT"])) $os = "OS/2"; elseif(@ereg("AIX", $_SERVER["HTTP_USER_AGENT"])) $os = "AIX"; else $os = "Other"; /* Save on the databases the obtained values */ $rs = $conn->Execute("UPDATE stats_counter SET Counter = Counter + 1 WHERE (Type = 'total' AND Variable = 'hits') OR (Variable = '".$browser."' AND Type = 'browser') OR (Variable = '".$os."' AND Type = 'os')"); /* Start Detailed Statistics */ $dot = date("d-m-Y-H"); $now = explode ("-",$dot); $nowHour = $now[3]; $nowYear = $now[2]; $nowMonth = $now[1]; $nowDate = $now[0]; $sql = "SELECT Year FROM stats_year WHERE Year = ".$nowYear.""; $resultyear = $conn->Execute($sql); $RecCount = $resultyear->RecordCount(); if ($RecCount <= 0) { $sql = "INSERT INTO stats_year VALUES (".$nowYear.", 0)"; $rs = $conn->Execute($sql); for ($i = 1; $i <= 12; $i++) { $rs = $conn->Execute("INSERT INTO stats_month VALUES (".$nowYear.", ".$i.", 0)"); if ($i == 1) $TotalDay = 31; if ($i == 2) { if (date("L") == true) { $TotalDay = 29; } else { $TotalDay = 28; } } if ($i == 3) $TotalDay = 31; if ($i == 4) $TotalDay = 30; if ($i == 5) $TotalDay = 31; if ($i == 6) $TotalDay = 30; if ($i == 7) $TotalDay = 31; if ($i == 8) $TotalDay = 31; if ($i == 9) $TotalDay = 30; if ($i == 10) $TotalDay = 31; if ($i == 11) $TotalDay = 30; if ($i == 12) $TotalDay = 31; for ($k = 1; $k <= $TotalDay; $k++) { $rs = $conn->Execute("INSERT INTO stats_date VALUES (".$nowYear.", ".$i.", ".$k.", 0)"); } } } $sql = "SELECT Hour FROM stats_hour WHERE (Year = ".$nowYear.") AND (Month = ".$nowMonth.") AND (Date = ".$nowDate.")"; $result = $conn->Execute($sql); $numrows = $result->RecordCount(); if ($numrows <= 0) { for ($i = 0; $i <= 23; $i++) { $rs = $conn->Execute("INSERT INTO stats_hour VALUES (".$nowYear.", ".$nowMonth.", ".$nowDate.", ".$i.", 0)"); } } $rs = $conn->Execute("UPDATE stats_year SET Hits = Hits + 1 WHERE Year = ".$nowYear.""); $rs = $conn->Execute("UPDATE stats_month SET Hits = Hits + 1 WHERE (Year = ".$nowYear.") AND (Month = ".$nowMonth.")"); $rs = $conn->Execute("UPDATE stats_date SET Hits = Hits + 1 WHERE (Year = ".$nowYear.") AND (Month = ".$nowMonth.") AND (Date = ".$nowDate.")"); $rs = $conn->Execute("UPDATE stats_hour SET Hits = Hits + 1 WHERE (Year = ".$nowYear.") AND (Month = ".$nowMonth.") AND (Date = ".$nowDate.") AND (Hour = ".$nowHour.")"); ?> - Finally, re-generate your script files using PHPMaker as always.
[/hidepost]
Leave a Reply
You must be logged in to post a comment.