This extension is a simple user counter, using MySQL für counting the number of visitors. It's a port of the pCounter from Andreas Droesch.
The counter supports the following data:
- users online
- total user of today
- total user of yesterday
- total user overall
- maximum user at a day
- date for the maximum
UserCounter does not use cookies or sessions. The count is only based on the IP address of users, but this information is stored as md5-hash in database.
With version 1.2 I have completely rewritten this component and added some new features. From now on you only have to copy the UserCounter.php
, add some settings to you config and everthing works fine.
- Copy
UserCounter.php
from folder 1.1 toprotected/components
orprotected/extensions
. - Open your config, in my case
protected/config/main.php
. - Add the component userCounter to the components-section, so it's accessable via
Yii::app()->userCounter
.
return array(
'components' => array(
'userCounter' => array(
// Use this when you copied the file to components folder
'class' => 'application.components.UserCounter',
// ... or this for extensions folder
'class' => 'ext.UserCounter',
// You can setup these options:
'tableUsers' => 'pcounter_users',
'tableSave' => 'pcounter_save',
'autoInstallTables' => true,
'onlineTime' => 10, // min
),
),
);
Please ensure that you use the correct class path and have a look at the options for UserCounter: tableUsers
, tableSave
, autoInstallTables
and onlineTime
. For further information go to documentation.
4. (optional) If you want UserCounter to update the user values automatically, you can add userCounter
to the preload
configuration. If you want to update it on your own, you have to call Yii::app()->userCounter->refresh()
:
return array(
'preload' => array('log', 'userCounter'),
);
- Copy
UserCounter.php
from folder 2.0 to your app folder, e.g./components
(basic template) or/frontend/components
(with advanced template). - Open your config, in my case it's
frontend/config/main.php
. - Add the component
userCounter
to the components-section, so it's accessable viaYii::$app->userCounter
.
return [
'components' => [
'userCounter' => [
'class' => 'app\components\UserCounter',
// You can setup these options:
'tableUsers' => 'pcounter_users',
'tableSave' => 'pcounter_save',
'autoInstallTables' => true,
'onlineTime' => 10, // min
],
],
];
Please ensure that you use the correct class path and have a look at the options for UserCounter: tableUsers
, tableSave
, autoInstallTables
and onlineTime
. For further information go to documentation.
4. (optional) If you want UserCounter to update the user values automatically, you can add userCounter
to the preload
configuration. If you want to update it on your own, you have to call Yii::$app->userCounter->refresh()
:
return [
'bootstrap' => ['log', 'userCounter'],
];
Here a very simple example how you can use UserCounter. This example shows you how you access every value provided by this component.
online: <?php echo Yii::app()->userCounter->getOnline(); ?><br />
today: <?php echo Yii::app()->userCounter->getToday(); ?><br />
yesterday: <?php echo Yii::app()->userCounter->getYesterday(); ?><br />
total: <?php echo Yii::app()->userCounter->getTotal(); ?><br />
maximum: <?php echo Yii::app()->userCounter->getMaximal(); ?><br />
date for maximum: <?php echo date('d.m.Y', Yii::app()->userCounter->getMaximalTime()); ?>
online: <?php echo Yii::$app->userCounter->getOnline(); ?><br />
today: <?php echo Yii::$app->userCounter->getToday(); ?><br />
yesterday: <?php echo Yii::$app->userCounter->getYesterday(); ?><br />
total: <?php echo Yii::$app->userCounter->getTotal(); ?><br />
maximum: <?php echo Yii::$app->userCounter->getMaximal(); ?><br />
date for maximum: <?php echo date('d.m.Y', Yii::$app->userCounter->getMaximalTime()); ?>
online: 9
today: 17
yesterday: 28
total: 1203
maximum: 32
date for maximum: 17.10.2009
UserCounter does not use any cookies or sessions to detect visits. It only consideres the IP address of the user, with all its pitfalls ‒ this component is meant to be a simple component. The IP address is stored as md5 hash, so privacy is considered.
-
Name of table, in which visitor information, IP address and last access timestamp, is stored.
Default:
pcounter_users
-
Name of table in which component statistics are stored.
Default:
pcounter_save
-
If
true
and tables does not exist, tables are installed to database on component initialization.Default:
true
-
Defines the time in minutes, how long a user is considered online without any further action.
Default:
10