Adam M.
Adam M.

Reputation: 773

Restricting Laravel Error Log by IP Address

When debug is set to true in Laravel's app/config.php is there any way to restrict the resultant Whoops error page with stack trace to certain IP addresses, and IPs not on that list being shown a specific view?

Thanks.

Upvotes: 3

Views: 3152

Answers (2)

LSerni
LSerni

Reputation: 57388

The config.php (or app/config/app.php) file is a PHP file like any other.

So nobody stops you from specifying

'debug' => in_array($_SERVER['REMOTE_ADDR'], array('192.168.0.1','127.0.0.1')),

which will result in debug being true from some IP addresses, but not others.

Update: if running from the CLI, we may want

'debug' => array_key_exists('REMOTE_ADDR', $_SERVER)
        ? in_array($_SERVER['REMOTE_ADDR'], array(
             '192.168.0.1',         /* List of allowed addresses */
             '127.0.0.1'
        ))
        : true /* Actually, whatever we want when going CLI */
        ;

You can even check for a "user password" embedded in the browser request (remember that it goes out in the clear unless you're using SSL; don't use "important" passwords).

'debug' => (strpos($_SERVER['HTTP_USER_AGENT'], 'KLAATU-BARADA-NIKTO') > 0),

and then use, say, a Firefox plug-in to edit your browser's User-Agent and add a password. Then your browser will have the debug, but no other.

Upvotes: 2

Jason Lewis
Jason Lewis

Reputation: 18665

Not built in no.

But you could probably implement this quite easily by capturing all exceptions and only re-throwing once you've compared the IP address of the user.

So in app/start/global.php you'd need to configure the "Application Error Handler". At the moment it captures all exceptions and simply logs them with Log::error. So in there you could compare the users IP address with an array of valid IP addresses:

App::error(function(Exception $exception, $code)
{
    Log::error($exception);

    $validIpAddresses = ['123.456.789.0', '321.654.987.0'];

    if (in_array(Request::getClientIp(), $validIpAddresses))
    {
        throw $exception;
    }

    return View::make('error');
});

Upvotes: 3

Related Questions