承接 esi/simple_counter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

esi/simple_counter

最新稳定版本:v6.0.1

Composer 安装命令:

composer require esi/simple_counter

包简介

A simple web hit counter.

README 文档

README

Build Status Code Coverage Scrutinizer Code Quality Tests PHPStan Psalm Static analysis Type Coverage Psalm Level Quality Gate Status Latest Stable Version Downloads per Month License

Simple Counter is a simple PHP counter that counts your website visitors. It has the ability to either show the count as plain text or images; and whether to count only unique hits, or all hits. (IP Based)

Important Note

  • As of v6.0.0, Simple Counter is no longer licensed under the GNU LGPLv3 license.
  • v6.0.0 is a complete rewrite of the library, and no GNU LGPLv3 licensed code remains.
  • v5.0.1 and previous are still licensed under the GNU LGPLv3 license.

With that being said, it is important to read the Upgrading section below if you are coming from an older version.

Acknowledgements

The icons used for the default image set (0-9 'png' images found in counter/images/) are licensed under the CC BY 4.0 DEED license, and were designed by StreamlineHQ.

Upgrading

Pre-v6 -> v6 is not a simple upgrade. Several things have changed in this rewrite, and there are breaking changes.

Please read UPGRADING.md first before attempting an upgrade.

Installation

To install Simple Counter, first install via composer:

composer require esi/simple_counter:^6.0

There are several options defined by default, however you will likely run into issues if you do not change some of them. More information can be found in Usage below.

  • Copy the counter directory from vendor/esi/simple_counter to your webroot
    • The counter directory contains the logs and images directory.
    • You can change the name of either directory if you wish, or skip using the counter directory all together and just move logs and images to your webroot.
      • However:
        • The ip file and counter file must remain ips.json and counter.json
        • The images must be named 0-9.
  • Make sure the ips.json and counter.json files within your logs directory are writable.

Usage

More detailed documentation is a work in progress.

Usage is fairly simple once installed. There is currently one option for the type of counter you wish to use, and that is the FlatfileStorage. A DatabaseStorage is slated for an upcoming release.

Simply add the following code to the page where you want the counter to be shown:

<?php

// Load the composer autoload file, if not already loaded
require_once 'vendor/autoload.php';

use Esi\SimpleCounter\Counter;
use Esi\SimpleCounter\Storage\FlatfileStorage;
use Esi\SimpleCounter\Configuration\FlatfileConfiguration;

/**
 * $options is an array of:
 *
 * array{
 *     logDir: string,
 *     countFile: string,
 *     ipFile: string,
 *     imageDir: string,
 *     imageExt: string,
 *     uniqueOnly: bool,
 *     asImage: bool,
 *     honorDnt: bool,
 *     visitorTextString?: string
 * }
 *
 * Default values are:
 *
 * [
 *      'logDir'            => dirname(__DIR__, 2) . '/counter/logs/',
 *      'countFile'         => 'counter.json',
 *      'ipFile'            => 'ips.json',
 *      'imageDir'          => dirname(__DIR__, 2) . '/counter/images/',
 *      'imageExt'          => '.png',
 *      'uniqueOnly'        => true,
 *      'asImage'           => false,
 *      'honorDnt'          => false,
 *      'visitorTextString' => 'You are visitor #%s',
 * ] 
 */
// Valid options are:
$options = [
    'logDir'            => '/path/to/some/dir/logs',
    'countFile'         => 'counter.json',
    'ipFile'            => 'ips.json',
    'imageDir'          => '/path/to/some/dir/images',
    'imageExt'          => '.png', // '.png', '.jpg' etc. default images are PNG images
    'asImage'           => true,   // true = images, false = plain text
    'uniqueOnly'        => true,   // true = counts only unique ip's, false = counts all,
    'honorDnt'          => false,
    'visitorTextString' => 'You are visitor #%s',
];

/**
 * Important note regarding the 'visitorTextString'. This is the text that is shown if 'asImage' is false.
 *
 * For example, by default, it would show: You are visitor #123.
 * If you wanted to change it to something like: Counter: #123,
 * you would set the 'visitorTextString' option to:
 * 
 * 'Counter: #%s'
 */

/**
 * When creating the counter instance, a Storage implementation with a valid Configuration is required.
 * Currently, Simple Counter ships with one Storage implementation, and it's corresponding Configuration:
 *
 * \Esi\SimpleCounter\Storage\FlatfileStorage
 * \Esi\SimpleCounter\Configuration\FlatfileConfiguration
 *
 * \Esi\SimpleCounter\Counter can be used as a wrapper, but it is not necessary. For example:
 *
 * $counter = new Counter(
 *     new FlatfileStorage(
 *         FlatfileConfiguration::initOptions($options)
 *     )
 * );
 */
// Pass custom options
$counter = new FlatfileStorage(
    FlatfileConfiguration::initOptions($options)
);

// ... or if you wish to use defaults
$counter = new FlatfileStorage(
    FlatfileConfiguration::initOptions()
);

// ... or maybe you only want to switch to using images, for example
$counter = new FlatfileStorage(
    FlatfileConfiguration::initOptions(['asImage' => true])
);

// Finally, call display(). You can either output it directly or save it to a variable if needed
echo $counter->display();

// ... or ...

$hitCount = $counter->display();

// ... do some stuff
echo $hitCount;

?>

Handling Errors

Simple Counter uses Exceptions for various issues that may arise throughout its process.

Currently, most exceptions fall under \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException.

These exceptions can be thrown if:

  • You pass an option that is not defined.
  • A given option's value type does not match the allowed types.
  • When reading/writing to a file encounters an error, since it is likely due to a file name/location issue.
  • For invalid directories or files, in terms of logDir, imageDir, countFile, ipFile

About

Requirements

  • PHP >= 8.2

Credits

Contributing

See CONTRIBUTING.

Bugs and feature requests are tracked on GitHub.

Contributor Covenant Code of Conduct

See CODE_OF_CONDUCT.md

Backward Compatibility Promise

See backward-compatibility.md for more information on Backwards Compatibility.

Changelog

See the CHANGELOG for more information on what has changed recently.

License

See the LICENSE for more information on the license that applies to this project.

Important Note

Simple Counter switched to the MIT license as of v6.0.

Version 5.0.1 and older is licensed under the GNU LGPL v3 License. See the <= 5.x LICENSE.md file for details.

Security

See SECURITY for more information on the security disclosure process.

统计信息

  • 总下载量: 36
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 26
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 26
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固