SilentShield

PHP integracija

Apsaugokite savo PHP formas su SilentShield. Šis vadovas apima Laravel, Symfony ir natūralų PHP.

1. Frontend nustatymas

Pridėkite SilentShield skriptą prie savo šablono (Blade, Twig arba paprastas HTML):

HTML + PHPphp
<script src="https://api.silentshield.io/client.js?k=<?= $apiKey ?>" crossorigin="anonymous" defer></script>

2a. Natūralaus PHP patikra

Patvirtinkite nonce pateikiant formą:

PHPphp
<?php
$nonce = $_POST['behavior_nonce'] ?? '';
if (empty($nonce)) {
    http_response_code(422);
    die('Missing verification nonce');
}

$ch = curl_init('https://api.silentshield.io/api/v1/captcha/verify-nonce');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'X-Api-Key: ' . SILENTSHIELD_API_KEY,
    ],
    CURLOPT_POSTFIELDS => json_encode(['nonce' => $nonce]),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 5,
]);
$result = json_decode(curl_exec($ch), true);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode !== 200 || ($result['ok'] ?? false) !== true
    || ($result['verdict'] ?? '') !== 'human') {
    http_response_code(403);
    die('Bot detected');
}

// Form is safe — continue processing
?>

2b. Laravel tarpinė programinė įranga

Sukurkite tarpinę programinę įrangą, kuri automatiškai patikrintų nonce visoms POST užklausoms:

Laravel Middlewarephp
<?php
// app/Http/Middleware/VerifySilentShield.php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class VerifySilentShield
{
    public function handle(Request $request, Closure $next)
    {
        $nonce = $request->input('behavior_nonce');
        if (!$nonce) {
            return response()->json(['error' => 'Missing nonce'], 422);
        }

        $response = Http::withHeaders([
            'X-Api-Key' => config('services.silentshield.key'),
        ])->timeout(5)->post(
            'https://api.silentshield.io/api/v1/captcha/verify-nonce',
            ['nonce' => $nonce]
        );

        if (!$response->successful()
            || $response->json('ok') !== true
            || $response->json('verdict') !== 'human') {
            return response()->json(['error' => 'Bot detected'], 403);
        }

        return $next($request);
    }
}

Užregistruokite tarpinę programinę įrangą

Pridėkite tarpinę programinę įrangą prie savo maršrutų bootstrap/app.php arba Kernel.php faile:

routes/web.phpphp
// routes/web.php
Route::post('/contact', [ContactController::class, 'submit'])
    ->middleware(VerifySilentShield::class);

WordPress

WordPress integracijai žiūrėkite specialų WordPress vadovą.