PHP code example of djl997 / simple-laravel-form-protection

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

    

djl997 / simple-laravel-form-protection example snippets


use Djl997\SimpleLaravelFormProtection\Concerns\UsesFormSubmission;

class ContactForm extends Component
{
    use UsesFormSubmission;

    protected string $form_key = 'contact';

    public function mount(): void
    {
        $this->mountMeta();
    }

    public function submit()
    {
        $this->validate();

        $submission = $this->createFormSubmission([
            'name' => $this->name,
            'email' => $this->email,
            'message' => $this->message,
        ]);

        if (! $submission->is_spam) {
            Mail::to('[email protected]')->send(new ContactFormSubmitted(...));
        }
    }
}

use Djl997\SimpleLaravelFormProtection\Facades\FormProtection;
use Djl997\SimpleLaravelFormProtection\Scoring\SpamScorer;

public function store(Request $request)
{
    $data = $request->validate([
        'name' => ['ssage($data['message']);

    $submission = FormProtection::record($request, 'contact', $data, $content_score);

    if (! $submission->is_spam) {
        Mail::to('[email protected]')->send(new ContactFormSubmitted($submission));
    }

    return back()->with('status', 'Bedankt voor je bericht!');
}

public function calculateContentScore(int $content_score = 0): int
{
    $content_score = $this->scoreEmail($this->email, $content_score);
    $content_score = $this->scorePhone($this->phone, $content_score);

    return $this->scoreMessage($this->message, $content_score);
}

public function spamScoreThreshold(): int
{
    return 4;
}

Schedule::command('model:prune')->daily();
Schedule::command('form-protection:purge')->daily();
bash
composer vendor:publish --tag=simple-laravel-form-protection-migrations
php artisan vendor:publish --tag=simple-laravel-form-protection-config
php artisan migrate

# optional, only to customise the <x-form-protection::meta /> markup
php artisan vendor:publish --tag=simple-laravel-form-protection-views
blade
<form method="POST" action="/contact">
    @csrf
    <x-form-protection::meta form-key="contact" />

    {{-- your fields --}}
</form>
bash
php artisan form-protection:purge          # honours pruning.spam_after_days
php artisan form-protection:purge --days=7