Last active
August 29, 2015 14:10
-
-
Save joubertredrat/2e2d045053293536b8ce to your computer and use it in GitHub Desktop.
Simple pageview counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple pageview class. | |
* | |
* Usage: | |
* \Counter::register(); | |
* \Counter::getStatus(); | |
* | |
* @author Joubert <me@redrat.com.br> | |
*/ | |
class Counter { | |
/** | |
* Register new pagevew. | |
* | |
* @return void | |
*/ | |
public static function register() { | |
$data['page'] = '"'.$_SERVER['REQUEST_URI'].'"'; | |
$data['date'] = '"'.date('Y-m-d H:i:s').'"'; | |
$query = 'INSERT INTO reports ('.implode(', ', array_keys($data)).') VALUES ('.implode(', ', $data).')'; | |
/* be happy here */ | |
} | |
/** | |
* Get result from pageviews. | |
* | |
* @return ? | |
*/ | |
public static function getStatus() | |
{ | |
$query = 'SELECT page, COUNT(*) AS total FROM reports GROUP BY page ORDER BY total DESC'; | |
/* be happy here */ | |
} | |
/* | |
Database example: | |
CREATE TABLE IF NOT EXISTS `reports` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`page` text NOT NULL, | |
`date` datetime NOT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ; | |
INSERT INTO `reports` (`id`, `page`, `date`) VALUES | |
(1, 'hev.php', '2014-11-11 11:30:26'), | |
(2, 'date.php', '2014-11-18 12:34:00'), | |
(3, 'date.php', '2014-11-26 14:25:33'), | |
(4, 'yuno.php', '2014-11-20 12:35:28'), | |
(5, 'hev.php', '2014-11-18 17:35:26'), | |
(6, 'a.php', '2014-11-18 17:35:26'), | |
(7, 'hev.php', '2014-11-26 00:00:00'), | |
(8, 'hev.php', '2014-11-11 11:30:26'); | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment