From f0027dd84e7dbc1056856da1548c8c8527562dbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 20 Jul 2014 18:10:47 +0200 Subject: [PATCH] [TEST] Added Hit counter --- www/protected/config/main.php | 5 + www/protected/extensions/UserCounter.php | 262 +++++++++++++++++++++++ www/protected/views/layouts/main.php | 2 + www/protected/views/msmain/about.php | 4 +- www/protected/views/msmain/admin.php | 11 + 5 files changed, 282 insertions(+), 2 deletions(-) create mode 100644 www/protected/extensions/UserCounter.php diff --git a/www/protected/config/main.php b/www/protected/config/main.php index bd9c00e..8037ef6 100644 --- a/www/protected/config/main.php +++ b/www/protected/config/main.php @@ -30,6 +30,7 @@ return ArrayX::merge( 'import' => [ 'application.models.*', + 'application.extensions.*', 'application.components.*', 'application.components.widgets.*', 'application.components.extendedGitGraph.*', @@ -124,6 +125,10 @@ return ArrayX::merge( [ 'tablePrefix' => 'ms4_', ], + + 'counter' => array( + 'class' => 'UserCounter', + ), ], // application-level parameters that can be accessed diff --git a/www/protected/extensions/UserCounter.php b/www/protected/extensions/UserCounter.php new file mode 100644 index 0000000..c3fa3e4 --- /dev/null +++ b/www/protected/extensions/UserCounter.php @@ -0,0 +1,262 @@ +cfg_tbl_users; + $cfg_tbl_save = $this->cfg_tbl_save; + $cfg_online_time = $this->cfg_online_time; + + // Daten aus DB auslesen + $sql = 'SELECT save_name, save_value FROM ' . $cfg_tbl_save; + $command = Yii::app()->db->createCommand($sql); + $dataReader = $command->query(); + $data = array(); + while (($row = $dataReader->read()) !== false) + { + $data[$row['save_name']] = $row['save_value']; + } + + // Aktuellen Tag als julianisches Datum + $today_jd = GregorianToJD(date('m'), date('j'), date('Y')); + + // Prüfen ob wir schon einen neuen Tag haben + if ($today_jd != $data['day_time']) + { + // Anzahl der Besucher von heute auslesen + $sql = 'SELECT COUNT(user_ip) AS user_count FROM ' . $cfg_tbl_users; + $command = Yii::app()->db->createCommand($sql); + $dataReader = $command->query(); + $row = $dataReader->read(); + $today_count = $row['user_count']; + + // Anzahl der Tage zum letzten Update ermitteln + $days_between = $today_jd - $data['day_time']; + + // Zählerwert von heute auf gestern setzen + $sql = 'UPDATE ' . $cfg_tbl_save . ' SET save_value=' . ($days_between == 1 ? $today_count : 0) . ' WHERE save_name="yesterday"'; + $command = Yii::app()->db->createCommand($sql); + $command->execute(); + + // Auf neuen Besucherrekord prüfen + if ($today_count >= $data['max_count']) + { + // Daten aktualisieren + $data['max_time'] = mktime(12, 0, 0, date('n'), date('j'), date('Y')) - 86400; + $data['max_count'] = $today_count; + + // Rekordwerd speichern + $sql= 'UPDATE ' . $cfg_tbl_save . ' SET save_value=' . $today_count . ' WHERE save_name="max_count"'; + $command = Yii::app()->db->createCommand($sql); + $command->execute(); + + // Aktuellen Tag als neuen Rekordtag speichern + $sql= 'UPDATE ' . $cfg_tbl_save . ' SET save_value=' . $data['max_time'] . ' WHERE save_name="max_time"'; + $command = Yii::app()->db->createCommand($sql); + $command->execute(); + } + + // Gesamtzähler erhöhen + $sql = 'UPDATE ' . $cfg_tbl_save . ' SET save_value=save_value+' . $today_count . ' WHERE save_name="counter"'; + $command = Yii::app()->db->createCommand($sql); + $command->execute(); + + // Alte Besucherdaten aus Tabelle entfernen + $sql = 'TRUNCATE TABLE ' . $cfg_tbl_users; + $command = Yii::app()->db->createCommand($sql); + $command->execute(); + + // Datum aktualisieren + $sql= 'UPDATE ' . $cfg_tbl_save . ' SET save_value=' . $today_jd . ' WHERE save_name="day_time"'; + $command = Yii::app()->db->createCommand($sql); + $command->execute(); + + // Daten aktualisieren + $data['counter'] += $today_count; + $data['yesterday'] = ($days_between == 1 ? $today_count : 0); + } + + // IP des Besuchers ermitteln + $user_ip = Yii::app()->db->quoteValue(isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']); + + // Besucher speichern oder aktualisieren + $sql = 'INSERT INTO ' . $cfg_tbl_users . ' VALUES ("' . $user_ip . '", ' . time() . ') ON DUPLICATE KEY UPDATE user_time=' . time(); + $command = Yii::app()->db->createCommand($sql); + $command->execute(); + + // Rückgabearray initialisieren + $output = array(); + + // Anzahl der heutigen Besucher auslesen + $sql = 'SELECT COUNT(user_ip) AS user_count FROM ' . $cfg_tbl_users; + $command = Yii::app()->db->createCommand($sql); + $dataReader = $command->query(); + $row = $dataReader->read(); + $output['today'] = $row['user_count']; + + // Gesamte Besucherzahl und Besucher vom Vortag zurückgeben + $output['counter'] = $data['counter'] + $output['today']; + $output['yesterday'] = $data['yesterday']; + + // Aktuelle Besucher der letzten x Minuten auslesen + $sql = 'SELECT COUNT(user_ip) AS user_count FROM ' . $cfg_tbl_users . ' WHERE user_time>=' . (time() - $cfg_online_time * 60); + $command = Yii::app()->db->createCommand($sql); + $dataReader = $command->query(); + $row = $dataReader->read(); + $output['online'] = $row['user_count']; + + // Wurde der aktuelle Besucherrekord heute überschritten? + if ($output['today'] >= $data['max_count']) + { + // Heutigen Tag als Rekord zurückgeben + $output['max_count'] = $output['today']; + $output['max_time'] = time(); + } + else + { + // Alten Rekord zurückgeben + $output['max_count'] = $data['max_count']; + $output['max_time'] = $data['max_time']; + } + + $this->user_total = $output['counter']; + $this->user_online = $output['online']; + $this->user_today = $output['today']; + $this->user_yesterday = $output['yesterday']; + $this->user_max_count = $output['max_count']; + $this->user_time = $output['max_time']; + } + + /** + * Getter für die Anzahl aller Besucher. + **/ + public function getTotal() + { + return $this->user_total; + } + + /** + * Getter für die Anzahl der User, die gerade Online sind. + **/ + public function getOnline() + { + return $this->user_online; + } + + /** + * Getter für die Anzahl der User, die heute online waren. + **/ + public function getToday() + { + return $this->user_today; + } + + /** + * Getter für die Anzahl der User, die Gestern online waren. + **/ + public function getYesterday() + { + return $this->user_yesterday; + } + + /** + * Getter für die maximale Anzahl der User, die an einem Tag online war. + **/ + public function getMaximal() + { + return $this->user_max_count; + } + + /** + * Getter für den Zeitpunkt, an dem die maximale Anzahl der User online war. + **/ + public function getMaximalTime() + { + return $this->user_time; + } + +} + + +?> diff --git a/www/protected/views/layouts/main.php b/www/protected/views/layouts/main.php index c71948a..8551e9e 100644 --- a/www/protected/views/layouts/main.php +++ b/www/protected/views/layouts/main.php @@ -1,5 +1,7 @@ +counter->refresh(); ?> + diff --git a/www/protected/views/msmain/about.php b/www/protected/views/msmain/about.php index a217ae9..794de20 100644 --- a/www/protected/views/msmain/about.php +++ b/www/protected/views/msmain/about.php @@ -22,9 +22,9 @@ $this->selectedNav = 'about';

Welcome to my private homepage.

-

My name is Mike Schwörer, and this is my homepage - here i upload program i write in my free time and sometimes i even write a blog entry.

+

My name is Mike Schwörer, and this is my homepage. Mainly I will upload my programs here and sometimes I write something for my blog

-

If you want you can look at the things I programd

+

If you want you can look at the things I programmed

loadFinishedData(); ?> diff --git a/www/protected/views/msmain/admin.php b/www/protected/views/msmain/admin.php index bf86f94..dd74904 100644 --- a/www/protected/views/msmain/admin.php +++ b/www/protected/views/msmain/admin.php @@ -123,4 +123,15 @@ $this->breadcrumbs = ); ?> + +
+

Hit counter

+ + online: counter->getOnline(); ?>
+ today: counter->getToday(); ?>
+ yesterday: counter->getYesterday(); ?>
+ total: counter->getTotal(); ?>
+ maximum: counter->getMaximal(); ?>
+ date for maximum: counter->getMaximalTime()); ?> +
\ No newline at end of file