vigstudio/laravel-stopforumspam
Laravel package that integrates StopForumSpam checks into your app to detect and block spammy registrations or requests by IP, email, or username. Includes easy configuration and middleware/validation-friendly helpers for quick spam protection.
Install the package:
composer require vigstudio/laravel-stopforumspam
Publish the config file:
php artisan vendor:publish --provider="Vigstudio\StopForumSpam\StopForumSpamServiceProvider" --tag="config"
Configure API Key:
Edit config/stopforumspam.php and add your StopForumSpam API key.
First Use Case: Check an email during registration:
use Vigstudio\StopForumSpam\Facades\StopForumSpam;
$email = 'spammer@example.com';
$result = StopForumSpam::checkEmail($email);
if ($result->isSpam()) {
abort(422, 'Email is flagged as spam.');
}
Form Request Validation:
use Vigstudio\StopForumSpam\Rules\Email;
use Vigstudio\StopForumSpam\Rules\Ip;
use Vigstudio\StopForumSpam\Rules\Username;
public function rules()
{
return [
'email' => ['required', new Email],
'ip' => ['required', new Ip],
'username' => ['required', new Username],
];
}
Custom Rule Logic:
public function passes($attribute, $value)
{
$result = StopForumSpam::checkEmail($value);
return !$result->isSpam() || config('stopforumspam.allow_suspicious');
}
Registration Flow:
$user = User::create([
'email' => $request->email,
'username' => $request->username,
]);
$ip = $request->ip();
$emailResult = StopForumSpam::checkEmail($user->email);
$ipResult = StopForumSpam::checkIp($ip);
if ($emailResult->isSpam() || $ipResult->isSpam()) {
$user->delete();
return back()->withErrors(['spam' => 'Account blocked due to spam flags.']);
}
Comment System:
$comment = new Comment();
$comment->user_id = auth()->id();
$comment->ip = $request->ip();
$comment->content = $request->content;
$ipResult = StopForumSpam::checkIp($comment->ip);
if ($ipResult->isSpam()) {
return back()->with('error', 'Your IP is flagged as spam.');
}
$result = Cache::remember("sfspam:{$email}", now()->addHours(24), fn() =>
StopForumSpam::checkEmail($email)
);
throttle middleware to limit API calls.if ($result->isSpam()) {
\Log::warning('Spam attempt detected', [
'type' => $result->type,
'value' => $value,
'score' => $result->score,
]);
}
API Rate Limits:
False Positives:
config('stopforumspam.threshold') to adjust sensitivity (default: 50). Lower values (e.g., 30) reduce false positives but may miss spam.IPv6 Support:
$ip = filter_var($request->ip(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
Username Checks:
min:3 length, alphanumeric).debug to true in config/stopforumspam.php to log raw API responses:
'debug' => env('SFSPAM_DEBUG', false),
StopForumSpam facade returns a Vigstudio\StopForumSpam\Response object with:
isSpam(): Boolean.score(): Numeric confidence (0–100).type(): "email", "ip", or "username".raw(): Full API response (for debugging).Custom Actions:
Override the default behavior by extending the StopForumSpam facade:
StopForumSpam::extend(function ($result) {
if ($result->score() > 70) {
event(new SpamDetected($result));
}
});
Local Overrides: Whitelist/blacklist entries locally to supplement StopForumSpam:
// config/stopforumspam.php
'local_whitelist' => [
'emails' => ['trusted@example.com'],
'ips' => ['192.168.1.1'],
],
Async Checks: Use Laravel Queues to offload checks for performance-critical paths:
dispatch(new CheckSpamJob($email, $ip, $username));
threshold: Minimum score to trigger a block (default: 50).allow_suspicious: If true, allows scores below threshold but logs them (default: false).'api_url' => env('SFSPAM_API_URL', 'https://www.stopforumspam.com/api'),
How can I help you explore Laravel packages today?