PHP code example of xaniashield / laravel

1. Go to this page and download the library: Download xaniashield/laravel 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/ */

    

xaniashield / laravel example snippets


use XaniaShield\Laravel\ShieldClient;

public function submit(Request $request, ShieldClient $shield)
{
    $verdict = $shield->checkRequest($request, [
        'email'           => $request->input('email'),
        'content'         => $request->input('message'),
        'form_identifier' => 'contact',
    ]);

    if ($verdict->isBlocked()) {
        // Silently drop, show an error, whatever fits your form.
        return back()->with('status', 'Message sent.');
    }

    // allow or challenge — proceed normally
}

use XaniaShield\Laravel\Facades\Shield;

$verdict = Shield::checkRequest($request, [
    'email'           => $request->input('email'),
    'form_identifier' => 'newsletter',
]);

$verdict->action();        // 'allow' | 'challenge' | 'block'
$verdict->score();         // 0-100
$verdict->isAllowed();     // bool
$verdict->isBlocked();     // bool
$verdict->shouldChallenge(); // bool
$verdict->reasons();       // string[]
$verdict->requestId();     // string|null
$verdict->failedOpen();    // bool — true if the API was unreachable

$verdict = $shield->checkRequest($request, [
    'email'          => $request->input('email'),
    'honeypot_field' => 'my_hp_field',
    'honeypot_value' => $request->input('my_hp_field', ''),
]);

Shield::configured(); // has an API key + https base URL
Shield::health();     // API reachable and key valid

Route::post('/contact', [ContactController::class, 'submit'])
    ->middleware('shield:contact');

use XaniaShield\Laravel\Rules\ShieldRule;

$request->validate([
    'email'   => ['

use XaniaShield\Laravel\Facades\Shield;

public function submit(Request $request)
{
    $verdict = Shield::checkRequest($request, [
        'email'           => $request->input('contact_email'),   // custom name
        'content'         => $request->input('enquiry_body'),
        'form_identifier' => 'enquiry',
        'honeypot_field'  => 'company_website',                  // custom honeypot
        'honeypot_value'  => $request->input('company_website', ''),
    ]);

    if ($verdict->isBlocked()) {
        return back()->withErrors(['enquiry_body' => 'Could not send. Please try again.']);
    }

    // proceed
}

use XaniaShield\Laravel\ShieldClient;

public function submit(ShieldClient $shield)
{
    $verdict = $shield->analyze([
        'email'           => $this->email,
        'content'         => $this->message,
        'form_identifier' => 'contact',
        'visitor_ip'      => request()->ip(),
    ]);

    if ($verdict->isBlocked()) {
        $this->addError('message', 'Could not send. Please try again.');
        return;
    }

    // proceed
}

use Illuminate\Foundation\Http\FormRequest;
use XaniaShield\Laravel\Rules\ShieldRule;

class ContactRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'email'   => ['

$verdict = Shield::checkRequest($request, [
    'email'           => $request->input('email'),
    'content'         => $request->input('body'),
    'form_identifier' => 'api-contact',
]);

if ($verdict->isBlocked()) {
    return response()->json(['message' => 'Rejected as spam.'], 422);
}
bash
php artisan vendor:publish --tag=shield-config
blade
{!! app(\XaniaShield\Laravel\ShieldClient::class)->timingField() !!}