yii-usercounter Simple user counter

  1. Overview
  2. Installation
  3. Usage
  4. Documentation
  5. Issues, questions & feedback
  6. Changelog
  7. Source

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 to protected/components or protected/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 the preloadconfiguration. If you want to update it on your own, you have to call Yii::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 via Yii::$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 the preloadconfiguration. If you want to update it on your own, you have to call Yii::$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 trueand 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 :)

Changelog

Changelog at GitHub

Source

yii-usercounter on GitHub

1 0
6 followers
648 downloads
Yii Version: all
License: (not set)
Category: Logging
Developed by: g3ck0
Created on: Feb 24, 2015
Last updated: 9 years ago

Downloads

show all

Related Extensions