drsoft/visitor-tracker 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

drsoft/visitor-tracker

Composer 安装命令:

composer require drsoft/visitor-tracker

包简介

README 文档

README

The Laravel Visitor Tracker package allows you to track guest and user activity on your web application. It provides detailed information about visitors, including their IP address, location, visited URLs, and more. This guide will walk you through the installation, configuration, and usage of the package.

Table of Contents

  1. Installation
  2. Configuration
  3. Middleware Setup
  4. Usage
  5. Database Structure
  6. Model Methods
  7. Customization
  8. Troubleshooting

Installation

  1. Install the package via Composer:

    composer require drsoft28/visitortracker
  2. Publish the configuration file and migrations:

    php artisan vendor:publish --provider="Drsoft28\VisitorTracker\VisitorTrackerServiceProvider"

    Or

    php artisan vendor:publish --tag=visitor-tracker-migrations --tag=visitor-tracker-config --tag=visitor-tracker-model

    This will create:

    • A visitortracker.php file in the config/ directory.
    • A migration file for the visitor_trackers table in the database/migrations/ directory.
  3. Run the migration to create the visitor_trackers table:

    php artisan migrate

Configuration

After publishing the configuration file, you can customize the package behavior by editing the config/visitortracker.php file. Here are the default options:

return [
    /*
    |--------------------------------------------------------------------------
    | Model
    |--------------------------------------------------------------------------
    |
    | This value is the model used for tracking visitors.
    | By default, it uses the package's built-in model.
    | You can replace it with your own model if needed.
    */
    'model' => \Drsoft28\VisitorTracker\Models\VisitorTracker::class,

    /*
    |--------------------------------------------------------------------------
    | Headers to Track
    |--------------------------------------------------------------------------
    |
    | Specify the headers to store in the `request_info` JSON column.
    | Add or remove headers as needed.
    */
    'headers' => [
        'HTTP_USER_AGENT',
        'HTTP_HOST',
        'SERVER_NAME',
        'SERVER_SOFTWARE',
        'REMOTE_ADDR',
        'HTTPS',
    ],
];

Middleware Setup

To start tracking visitors, you need to add the VisitorTrackerMiddleware to your application's middleware stack.

For Laravel 10 and Below:

Add the middleware to the web group in app/Http/Kernel.php:

'web' => [
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\VerifyCsrfToken::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
    \Drsoft28\VisitorTracker\Middleware\VisitorTrackerMiddleware::class,
],

For Laravel 11 and Above:

Add the middleware in bootstrap/app.php:

->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
        \Drsoft28\VisitorTracker\Middleware\VisitorTrackerMiddleware::class,
    ]);
})

Usage

Once the middleware is enabled, the package will automatically track visitors and store their information in the visitor_trackers table.

Default Model

By default, the package uses the \Drsoft28\VisitorTracker\Models\VisitorTracker model. You can change this in the visitortracker.php configuration file.

Database Columns

The visitor_trackers table contains the following columns:

  • user_id: The ID of the authenticated user (if applicable).
  • host_schema: The protocol (e.g., http or https).
  • host: The hostname (e.g., example.com).
  • ip: The visitor's IP address.
  • path: The visited path (e.g., /about).
  • full_url: The full URL visited.
  • url: The relative URL.
  • country_name: The visitor's country name.
  • country_code: The visitor's country code.
  • region_name: The visitor's region name.
  • region_code: The visitor's region code.
  • city_name: The visitor's city name.
  • zip_code: The visitor's ZIP code.
  • iso_code: The ISO code of the visitor's location.
  • latitude: The visitor's latitude.
  • longitude: The visitor's longitude.
  • timezone: The visitor's timezone.
  • referer: The referer URL.
  • route_name: The name of the route visited.
  • route_params: The parameters of the route.
  • request_info: A JSON column containing additional request information (e.g., headers).

Model Methods

The VisitorTracker model provides the following methods for querying visitor data:

  1. visitorsWithinSeconds($seconds):

    • Retrieve visitors who visited within the last $seconds seconds.
  2. visitorsWithinMinutes($minutes):

    • Retrieve visitors who visited within the last $minutes minutes.
  3. visitorsWithinHours($hours):

    • Retrieve visitors who visited within the last $hours hours.

Example Usage:

use Drsoft28\VisitorTracker\Models\VisitorTracker;

// Get visitors from the last 5 minutes
$recentVisitors = VisitorTracker::visitorsWithinMinutes(5)->get();

Customization

Custom Model

If you want to use a custom model for tracking visitors, follow these steps:

  1. Create a new model in your app/Models directory:

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class CustomVisitorTracker extends Model
    {
        protected $table = 'visitor_trackers';
        protected $fillable = [
            'user_id', 'host_schema', 'host', 'ip', 'path', 'full_url', 'url',
            'country_name', 'country_code', 'region_name', 'region_code', 'city_name',
            'zip_code', 'iso_code', 'latitude', 'longitude', 'timezone', 'referer',
            'route_name', 'route_params', 'request_info',
        ];
    }
  2. Update the visitortracker.php configuration file to use your custom model:

    'model' => \App\Models\CustomVisitorTracker::class,

Custom Headers

You can customize the headers tracked in the request_info column by updating the headers array in the visitortracker.php configuration file.

Troubleshooting

  1. Middleware Not Tracking Visitors:

    • Ensure the middleware is added to the correct group (web or global).
    • Verify that the middleware is not being skipped by other middleware.
  2. Migration Errors:

    • Ensure the visitor_trackers table does not already exist before running the migration.
    • Check for any typos in the migration file.
  3. Configuration Not Applied:

    • Clear the configuration cache after updating the visitortracker.php file:
      php artisan config:clear

Support

If you encounter any issues or have questions, please open an issue on the GitHub repository.

Thank you for using Laravel Visitor Tracker! 🚀

drsoft/visitor-tracker 适用场景与选型建议

drsoft/visitor-tracker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 95 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「laravel」 「visitor tracker」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 drsoft/visitor-tracker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 drsoft/visitor-tracker 我们能提供哪些服务?
定制开发 / 二次开发

基于 drsoft/visitor-tracker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-19