3

I have an array that I pull data from.

BLOCK_IP_LIST = [127.0.0.1,127.0.0.2,127.0.0.3]

I'm not sure how to do that.

I have Use .env BLOCK_IP_LIST in BlockIpMiddleware using Config>app.php

Config.app.php code like

'block_ip' => env('BLOCK_IP_LIST'),

my BlockIpMiddleware Code Like

class BlockIpMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        $blockIps = config('app.block_ip');

        if (in_array($request->ip(), $blockIps)) {
            return response()->json(['message' => "You don't have permission to access this website."]);
        }
        return $next($request);
    }
}

3 Answers 3

8

Your BlockIpMiddleware is alright

but .env should look like that

BLOCK_IP_LIST=127.0.0.1,127.0.0.2,127.0.0.3

Inside app.php

'block_ip' => explode(',', env('BLOCK_IP_LIST')),

explode find , and convert from string to array.


My opinion

You should do it with the database and cache it forever Because you/client can add/delete the IPs as you want and anytime.

Sign up to request clarification or add additional context in comments.

Comments

5

you can save your data as json in your .env file

BLOCK_IP_LIST={"127.0.0.1","192.168.1.2"}

and in your controller you can access your data using

$black_lists = json_decode(env('BLOCK_IP_LIST')); 

4 Comments

IMO this answer is better than the accepted answer because the accepted answer cannot handle empty arrays, so if you had BLAH= in your env file with no value assigned, explode(',', '') would yield [''], which is not the intended value. However, if you're happy to designate an empty array in your env file with BLAH=[] (which I think is a good idea because it tells the next dev information about how to populate the variable if they need to use it), when you json_decode() it you will end up with the empty array, as specified in the env file. So I think this a better general purpose solution.
@JamesG I agree with you that JSON looks like a much cleaner solution, but I don't understand why would anyone put an empty variable in the first place. Have you found yourself in this situation before ?
It might be set to a blank value in .env.dev or .env.testing, which might cause an unexpected result. Although that is a highly unlikely possibility, my point is more than the json version solves the problem and also handles this case, whereas the explode option doesn't handle this case. So if you want a solution that is closer to perfect (even if you don't need perfect) then the json solution is better. Having said that, the json version requires a lot more characters and contains more visual clutter, which itself can be undesirable, because it can make errors in the data harder to spot.
The JSON solution also allows you to create more complex arrays, such as those that require key/value entries. However, use with caution since you are now demanding that the creator of this value understand JSON.
0

It's bad to store array in .env files as it is meant to hold small text values only, storing and array of string, will most likely means this list will grow overtime. but to answer your question there is a hack to get it done.

.ENV file

BLOCK_IP_LIST = "127.0.0.1,127.0.0.2,127.0.0.3"

Now for your Config > app.php file:

'block_ip' => explode(',', env('BLOCK_IP_LIST')),

That should get you up and running, remember to clear your config cache.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.