CodeIgniter Forums
How block IP in PHP? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How block IP in PHP? (/showthread.php?tid=75311)



How block IP in PHP? - omid_student - 01-25-2020

Hi
I need block ip with PHP not firewall or htaccess
I know that i can forbidden user with send status to header
Is there other way?


RE: How block IP in PHP? - includebeer - 01-25-2020

You can create a filter that always redirect to a page with an error message when you detect the IP addresses you want to block:
https://codeigniter4.github.io/userguide/incoming/filters.html


RE: How block IP in PHP? - InsiteFX - 01-25-2020

@includebeer, he's using CodeIgniter 3.

You can check when they access the site by using CodeIgniter's ip_address() method.

Store the blocked IP's in the database load them on startup into an array then get the
users ip using ip_address() and loop through the blocked ip array to see if it's in there.


RE: How block IP in PHP? - jreklund - 01-25-2020

(01-25-2020, 09:00 AM)InsiteFX Wrote: users ip using ip_address() and loop through the blocked ip array to see if it's in there.
I would use the in_array function here. For readability and a little bit faster.


RE: How block IP in PHP? - omid_student - 01-25-2020

(01-25-2020, 07:00 AM)includebeer Wrote: You can create a filter that always redirect to a page with an error message when you detect the IP addresses you want to block:
https://codeigniter4.github.io/userguide/incoming/filters.html

But i use Codeigniter 3
Codeigniter 4 is hard to learning now Confused

(01-25-2020, 09:00 AM)InsiteFX Wrote: @includebeer, he's using CodeIgniter 3.

You can check when they access the site by using CodeIgniter's ip_address() method.

Store the blocked IP's in the database load them on startup into an array then get the
users ip using ip_address() and loop through the blocked ip array to see if it's in there.
Yes it is good way but it busy database for many IP


RE: How block IP in PHP? - jreklund - 01-26-2020

Option 1: Store in database and upon reading them - store them in a cache engine e.g. Redis for X time.
Option 2: Store them in PHP-file.

You can use hooks for both options.

If you don't want to display any message just terminate it with exit;.


RE: How block IP in PHP? - omid_student - 01-26-2020

(01-26-2020, 12:30 AM)jreklund Wrote: Option 1: Store in database and upon reading them - store them in a cache engine e.g. Redis for X time.
Option 2: Store them in PHP-file.

You can use hooks for both options.

If you don't want to display any message just terminate it with exit;.

A question?
If i add IP to htaccess file
Is is working? or need to restart apache?


RE: How block IP in PHP? - jreklund - 01-26-2020

Your .htaccess will be read on any new request. So you can block new ip-adressers with it.


RE: How block IP in PHP? - omid_student - 01-26-2020

(01-26-2020, 01:26 AM)jreklund Wrote: Your .htaccess will be read on any new request. So you can block new ip-adressers with it.

Oh Thanks it is finally solution