Welcome Guest, Not a member yet? Register   Sign In
How block IP in PHP?
#1

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?
Reply
#2

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...lters.html
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(This post was last modified: 01-25-2020, 09:01 AM by InsiteFX.)

@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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(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.
Reply
#5

(This post was last modified: 01-25-2020, 11:08 PM by omid_student.)

(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...lters.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
Reply
#6

(This post was last modified: 01-26-2020, 12:33 AM by jreklund.)

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;.
Reply
#7

(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?
Reply
#8

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

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB