Level 20
try using this
$allowedIP = 'your_website2_ip';
if ($_SERVER['SERVER_ADDR'] !== $allowedIP) {
return response()->json(['error' => 'Unauthorized'], 401);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello, In Laravel, I have an API. I'm currently utilizing this API on "website 2". How can I ensure that the API only accepts requests from the IP address of "website 2"?
I tried
if ($request->ip() !== $allowedIP) {
return response()->json(['error' => 'Unauthorized'], 401);
}
but $request->ip() gives user ip not server
Please or to participate in this conversation.