Laravel bot detection and email validation
Route middleware that blocks datacenter traffic before it reaches your controllers, and a validation rule that rejects disposable signup addresses.
1. Install
composer require guzzlehttp/guzzle
2. Add the file
app/Http/Middleware/BlockBots.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class BlockBots
{
public function handle(Request $request, Closure $next)
{
try {
$response = Http::withToken(config('services.turing.key'))
->timeout(3)
->post('https://uat.turing-api.com/v1/bot-check', [
'ip' => $request->ip(),
'user_agent' => $request->userAgent() ?? '',
]);
} catch (\Throwable $e) {
// Fail open: an outage on our side must never take your site down.
Log::warning('turing unreachable', ['error' => $e->getMessage()]);
return $next($request);
}
if (! $response->successful()) {
return $next($request);
}
if ($response->json('is_bot') === true) {
return response()->json([
'error' => 'Automated traffic is not permitted.',
'reasons' => $response->json('reasons') ?? [],
], 403);
}
return $next($request);
}
}3. Wire it up
// bootstrap/app.php (Laravel 11+)
->withMiddleware(function (Middleware $middleware) {
$middleware->alias(['bot' => \App\Http\Middleware\BlockBots::class]);
})
// routes/web.php
Route::post('/signup', SignupController::class)->middleware('bot');
// config/services.php
'turing' => ['key' => env('TURING_API_KEY')],4. Verify
Confirm the API answers before you debug your Laravel
wiring. The sample address sits inside a published AWS range, so a correct setup
returns is_bot: true.
curl -X POST https://uat.turing-api.com/v1/bot-check \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"ip":"52.1.2.3","user_agent":"curl/8.4.0"}'
import requests
HEADERS = {"Authorization": "Bearer YOUR_API_KEY"}
response = requests.post(
"https://uat.turing-api.com/v1/bot-check",
headers=HEADERS,
json={"ip": "52.1.2.3", "user_agent": "curl/8.4.0"},
timeout=5,
)
response.raise_for_status()
print(response.json())
const response = await fetch("https://uat.turing-api.com/v1/bot-check", {
method: "POST",
headers: {
Authorization: "Bearer YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({"ip":"52.1.2.3","user_agent":"curl/8.4.0"}),
signal: AbortSignal.timeout(5000),
});
if (!response.ok) throw new Error("turing: " + response.status);
console.log(await response.json());
$curl = curl_init("https://uat.turing-api.com/v1/bot-check");
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => '{"ip":"52.1.2.3","user_agent":"curl/8.4.0"}',
CURLOPT_HTTPHEADER => [
"Authorization: Bearer YOUR_API_KEY",
"Content-Type: application/json",
],
]);
$body = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
curl_close($curl);
$verdict = $status === 200 ? json_decode($body, true) : null;
var_dump($verdict);
require "net/http"
require "json"
uri = URI("https://uat.turing-api.com/v1/bot-check")
request = Net::HTTP::Post.new(uri)
request["Authorization"] = "Bearer YOUR_API_KEY"
request["Content-Type"] = "application/json"
request.body = { ip: "52.1.2.3", user_agent: "curl/8.4.0" }.to_json
response = Net::HTTP.start(uri.hostname, uri.port,
use_ssl: uri.scheme == "https",
open_timeout: 5, read_timeout: 5) do |http|
http.request(request)
end
puts JSON.parse(response.body)
Notes
- Put TURING_API_KEY in .env, never in config that is committed to git.
- Apply the middleware to write routes such as signup, checkout and comment rather than globally. You pay one credit per call.