Get the client IP address in Laravel – Fast tips

Valerio Barbera

In this article, I will share with you how to get the client IP address using the Laravel Request class.

You have three options to invoke the \Illuminate\Http\Request class:

// Using the helper function
echo request()->ip();
// Using the Facade
echo \Request::ip();
// Using the underlying method directly
echo \Request::getClientIp(true);

How to use the IP address to monitor your navigation sessions

The Inspector middleware allows you to exlude traffic from your monitoring data with a simple configuration:

/*
 |---------------------------------------------------------------------
 | Web request url to ignore
 |---------------------------------------------------------------------
 |
 | Add at this list the url schemes that you don't want monitoring
 | in your Inspector dashboard. You can also use wildcard expression (*).
 |
 */
 
'ignore_url' => [
    'telescope*',
    'vendor/telescope*',
    'horizon*',
    'vendor/horizon*',
],

If you need to implement a custom logic, the middleware is also designed to be extended.

<?php
use \Inspector\Laravel\Middleware\WebRequestMonitoring;

class InspectorFilterMonitoringMiddleware extends WebRequestMonitoring
{
    /**
     * Determine if Inspector should record current request.
     *
     * @param \Illuminate\Http\Request $request
     * @return bool
     */
    protected function shouldRecorded($request): bool
    {
        // Monitor requests coming from your IP addresses (e.g. for debugging purpose)
        return in_array($request->ip(), ['xxx.xxx.xxx.xxx']);
    }
}

Overwriting the shouldRecorded() method, you can implement a custom condition to decide which routes should be ignored by Inspector.

Check out the complete tutorial in the official documentation:

https://docs.inspector.dev/guides/laravel/http-requests-monitoring#ignore-http-requests

New to Inspector? Try it for free now

I hope this article can help you make better decisions for the design of your application.

Are you responsible for application development in your company? Consider trying my product Inspector to find out bugs and bottlenecks in your code automatically. Before your customers stumble onto the problem.

Inspector is usable by any IT leader who doesn’t need anything complicated. If you want effective automation, deep insights, and the ability to forward alerts and notifications into your messaging environment try Inspector for free. Register your account.

Or learn more on the website: https://inspector.dev

Related Posts

php-iterators-inspector

PHP Iterators for walking through data structures – FastTips

PHP Iterators are essential tools for efficiently traversing and manipulating data structures like arrays, objects, and more. They provide a clean and memory-efficient way to work with large datasets without loading the entire dataset into memory at once. In this tutorial, we will explore PHP iterators and how to use them for walking through various

Adoption of AWS Graviton ARM instances (and what results we’ve seen)

Working in software and cloud services you’ve probably already heard about the launch of new the Graviton machines based on custom ARM CPUs from AWS (Amazon Web Services).  In this article you can learn the fundamental differences between ARM and x86 architecture and the results we’ve achieved after the adoption of Graviton ARM machines in

Announcing increased data retention for monitoring data

Long story short: In the last 2 months of work we’ve achieved great results in cost optimization by refactoring both our infrastructure and code architecture, and we want to pass this value to you in the form of a longer data retention for your monitoring data. Thanks to these changes we are increasing our computational