Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

samehdev's avatar

how get client ip from Exception Handler / Throwable

I have one of the sites built by laravel and in the error log on the hosting and the log of errors in Laravel there are many attempts to access the .env file So how do I find out the IP of this person trying to access so that I can block him

I protected the .env file with the .htaccess file

But despite that, I tried to get the ip of this person, but I could not, as he is getting the ip of my hosting

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            $errorLog = collect([
                [
                    'file' => $e->getFile(),
                    'line' => $e->getLine(),
                    'code' => $e->getCode(),
                    'ip' => request()->getClientIp(),
                ]
            ])->concat(collect($e->getTrace())->take(4))->toArray();
            if (config('app.env') === 'production') {
                Log::channel('slack')->error($e->getMessage(), $errorLog);
            }
        });
    }

Is there any help or idea about this?

0 likes
1 reply
crnkovic's avatar

$request->ip() should return the IP address but that depends how you hosted your app and whether you use Cloudflare in front.

Please or to participate in this conversation.