These examples introduce the basic concepts of Phase 3 using pure PHP only,
with RedisSecurityGuard as the storage backend.
This file is intentionally simple.
It does not cover MySQL or MongoDB.
Developers who need framework integration or multi-driver examples
should read:
docs/examples/phase3_driver_usage.md
Before using any driver, the Security Guard library must load:
- Composer autoload
- Environment configuration
- Database resolver
- Redis adapter (from your project's profiles)
<?php
require __DIR__ . '/../../../vendor/autoload.php';
// ---------------------------------------------------------------------
// 🔧 Load adapter configuration
// ---------------------------------------------------------------------
$config = new EnvironmentConfig(__DIR__ . '/../../../');
$resolver = new DatabaseResolver($config);
// ---------------------------------------------------------------------
// 🟢 Resolve Redis adapter (Phase 3 basic examples)
// ---------------------------------------------------------------------
$redisAdapter = $resolver->resolve('redis.security', autoConnect: true);
// Shared logical identifier for all examples
$identifier = 'login.guard';$driver = new RedisSecurityGuard($redisAdapter, $identifier);The $identifier ensures all counters, blocks, and statistics
belong to one logical security domain (e.g., login system).
$attempt = new LoginAttemptDTO(
ip: '192.168.1.50',
subject: 'user@example.com',
resetAfter: 900,
userAgent: 'Mozilla/5.0'
);
$count = $driver->recordFailure($attempt);
echo "New failure count: {$count}\n";What happens internally (simplified):
- Redis increments a counter
- Applies/reset TTL (
resetAfter) - Returns current attempt count
$driver->resetAttempts(
ip: '192.168.1.50',
subject: 'user@example.com'
);
echo "Attempts reset.\n";This clears the counter for the given subject/IP pair.
$block = new SecurityBlockDTO(
ip: '192.168.1.50',
subject: 'user@example.com',
type: BlockTypeEnum::AUTO,
createdAt: time(),
expiresAt: time() + 3600 // 1 hour block
);
$driver->block($block);$driver->unblock(
ip: '192.168.1.50',
subject: 'user@example.com'
);$stats = $driver->getStats(
ip: '192.168.1.50',
subject: 'user@example.com'
);
print_r($stats);A typical output:
[
'attempts' => 2,
'blocked' => true,
'block_expires_at' => 1733848800
]- How to bootstrap the adapter system
- How to resolve a Redis adapter
- How to instantiate
RedisSecurityGuard - How to record failures, reset attempts, block, unblock, and read stats
- All with pure PHP and minimal complexity
For real-world integration (Slim, Laravel) and full multi-driver examples (MySQL + Redis + MongoDB), continue to: