Only allow certain ip addresses to register a user in Laravel 5.2

// .env

WHITELIST='192.168.1.1;192.168.1.2'

// access.php

whitelist => env('WHITELIST')

// WhitelistMiddleware.php

class WhitelistMiddleware
{
 /**
  * @param Request $request
  * @param Closure $next
  * @return \Illuminate\Http\RedirectResponse
  * @throws ForbiddenException
  */
   public function handle($request, Closure $next)
   {

     $whitelist = config('access.whitelist');

     $ipAddresses = explode(';', $whitelist);

      if (! in_array($request->ip(), $ipAddresses)) {

        \Log::error('IP address is not whitelisted', ['ip address', $request->ip()]);

        return redirect('home');
     }

     return $next($request);
  }
}

Instead checking in controller please check this way

php artisan make:middleware IpMiddleware

Code

<?php

namespace App\Http\Middleware;

use Closure;

class IpMiddleware
{

    public function handle($request, Closure $next)
    {
        if ($request->ip() != "192.168.0.155") {
        // here instead of checking a single ip address we can do collection of ips
        //address in constant file and check with in_array function
            return redirect('home');
        }

        return $next($request);
    }

}

then add the new middleware class in the $middleware property of your app/Http/Kernel.php class.

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'ipcheck' => \App\Http\Middleware\IpMiddleware::class,
];

then apply middelware to routes

Route::get('/', ['middleware' => ['ipcheck'], function () {
    // your routes here
}]);

I hope this helps!