PHP code example of pepeiborra / ci4-traffic-reader

1. Go to this page and download the library: Download pepeiborra/ci4-traffic-reader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pepeiborra / ci4-traffic-reader example snippets


use Pepeiborra\CI4TrafficReader\Filters\TrackVisitFilter;

public array $aliases = [
    // ... tus filtros existentes
    'trackVisit' => TrackVisitFilter::class,
];

// Aplicar a todas las rutas web:
public array $globals = [
    'after' => [
        'trackVisit',
    ],
];

use Pepeiborra\CI4TrafficReader\Controllers\AuditDashboard;

$routes->get('traffic-reader/visitas', [AuditDashboard::class, 'index'], [
    'filter' => 'login',   // tu filtro de autenticación
    'as'     => 'traffic-reader.visits',
]);

 namespace Config;

use Pepeiborra\CI4TrafficReader\Config\TrafficReader as BaseConfig;

class TrafficReader extends BaseConfig
{
    // Emails de alerta (separados por coma)
    public string $alertEmails  = '';   // o usar $_ENV

    // Slack webhook
    public string $slackWebhook = '';

    // Carpeta de logs dentro de WRITEPATH
    public string $storageFolder = 'traffic_reader';

    // Retención de logs en días
    public int $logRetentionDays = 30;

    // Umbrales
    public int $rateThreshold       = 60;   // req/min por IP
    public int $notFoundThreshold   = 20;   // 404/hora por IP
    public int $probeThreshold      = 3;    // accesos sensibles/hora
    public int $bruteForceThreshold = 10;   // 401-403/hora por IP

    // Rutas a excluir del tracking
    public array $excludePaths = ['api/health'];

    // Dashboard
    public string  $dashboardPrefix  = 'traffic-reader';
    public array   $dashboardFilters = ['login'];
    public string  $dashboardTitle   = 'Dashboard de Visitas';
    public ?string $dashboardLayout  = null; // null = standalone HTML
}

public function __construct()
{
    parent::__construct();
    $this->alertEmails  = $_ENV['TRAFFIC_READER_ALERT_EMAILS']  ?? '';
    $this->slackWebhook = $_ENV['TRAFFIC_READER_SLACK_WEBHOOK'] ?? '';
}

use Pepeiborra\CI4TrafficReader\Services\VisitsLogReader;

$reader  = new VisitsLogReader();
$dates   = $reader->availableDates();
$records = $reader->records('2025-04-23');
$stats   = $reader->stats($records);
$threats = $reader->threats('2025-04-23');

ci4-traffic-reader/
├── composer.json
├── README.md
└── src/
    ├── TrafficReaderRegistrar.php
    ├── Config/
    │   └── TrafficReader.php
    ├── Controllers/
    │   └── AuditDashboard.php
    ├── Filters/
    │   └── TrackVisitFilter.php
    ├── Services/
    │   ├── VisitsLogReader.php
    │   └── ThreatMailer.php
    └── Views/
        └── dashboard/
            └── index.php