Overview ¶
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.
Installation ¶
Yii 1.1 ¶
- 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 section.
- (optional) If you want UserCounter to update the user values automatically, you can add
userCounter
to thepreload
configuration. If you want to update it on your own, you have to callYii::app()->userCounter->refresh()
:
return array(
'preload' => array('log', 'userCounter'),
);
Yii 2.0 ¶
- 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.
- (optional) If you want UserCounter to update the user values automatically, you can add
userCounter
to thepreload
configuration. If you want to update it on your own, you have to callYii::$app->userCounter->refresh()
:
return [
'bootstrap' => ['log', 'userCounter'],
];
Usage ¶
Yii 1.1 ¶
Here a very simple example how you can use UserCounter. This example shows you how you access every value provided by this component.
~~~
[html]
online: <?php echo Yii::app()->userCounter->getOnline(); ?>
today: <?php echo Yii::app()->userCounter->getToday(); ?>
yesterday: <?php echo Yii::app()->userCounter->getYesterday(); ?>
total: <?php echo Yii::app()->userCounter->getTotal(); ?>
maximum: <?php echo Yii::app()->userCounter->getMaximal(); ?>
date for maximum: <?php echo date('d.m.Y', Yii::app()->userCounter->getMaximalTime()); ?>
~~~
Yii 2.0 ¶
[html]
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()); ?>
Result ¶
online: 9
today: 17
yesterday: 28
total: 1203
maximum: 32
date for maximum: 17.10.2009
Documentation ¶
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.
Options ¶
tableUsers
¶Name of table, in which visitor information, IP address and last access timestamp, is stored.
Default:
pcounter_users
tableSave
¶Name of table in which component statistics are stored.
Default:
pcounter_save
autoInstallTables
¶If
true
and tables does not exist, tables are installed to database on component initialization.Default:
true
onlineTime
¶Defines the time in minutes, how long a user is considered online without any further action.
Default:
10
Issues, questions & feedback ¶
Please post everything in the yii-usercounter extension thread in the forum, so we discuss it there. The comment section is not made for that. Thanks :)
WebApplication.counter is not difined??
i got error like this wt do??
Solution
Oh, this was a documentation issue. I have fixed it. You have to use userCounter instead of counter property.
// Wrong: <?php echo Yii::app()->counter->getOnline(); ?> // Right: <?php echo Yii::app()->userCounter->getOnline(); ?>
Sorry for that!
For further issues, please have a look at the thread in Yii forum.
i am running my application in local machine?so i can get
i am running my application in local machine..so can i get online users??
What exactly do you mean?
What exactly do you mean? If you access the application from the computer where you run the application, your access is counted via the local IP. If you access the application via network by another computer, this access is counted seperatly.
Does this help you?
hi g3ck0
i got it correctly.thanks...
Suggestion .
Suggestion : add Returned Visitor
sample: <?php echo Yii::app()->userCounter->getReturned(); ?>
Your suggestion
Hey Nimaa,
I'm sorry, but I won't implement that feature. The thing is that it's a simple user counter. Implementing such a feature needs some more technics, especially a cookie on client side, and then it's not that simple any more.
Furthermore, I want to provide a component that is not based on cookies, because of privacy protection reasons. I know, without that it's not that precise any more, but for more information about users, have a look at Google Analytics or, which I use too, Piwik.
Suggestion
Right
'preload' => array('userCounter'),
Wrong:
'preload' => array('counter'),
Error fixed!
Hey,
thanks for your comment! Yes, you are right! This was wrong in this documentation. I fixed it.
Yii2 version
Somebody know, when will release it wonderful extension for Yii2 version?
New version 1.3.0 -- Support for Yii2!
Hey Jakira!
I published a new version right now with Yii2 support. Please checkout this new version and tell me if it works for you?! :)
Thank you for Yii2 support
Thank You! Good work. It work's!
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.