Skip to content

LX4777/real-client-ip-to-laravel-octane

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Solution for Getting the Real User's IP in Laravel Octane

When using Laravel Octane, the $request->getClientIp() method returns the IP address of your host instead of the real user's IP address.

To forward the user's real IP address in Laravel Octane, you can create a middleware:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class OctaneTrustProxies
{
    public function handle(Request $request, Closure $next): Response
    {
        if ($request->header('x-forwarded-for')) {
            $clientIps = explode(',', $request->header('x-forwarded-for'));
            $request->server->set('REMOTE_ADDR', trim(end($clientIps)));
        }

        return $next($request);
    }
}

Then, add your middleware to Kernel.php in the middleware array, before the middlewares that require the real user's IP address:

    protected $middleware = [
        \App\Http\Middleware\OctaneTrustProxies::class,
        // middlewares that require the real user's IP address

    ];

About

Solution for Getting the Real User's IP in Laravel Octane

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published