Page 1 of 2

(PHP) Ban Ip From Your Site

Posted: Sat May 05, 2007 8:43 am
by anish
It is one of the effective code for banning the IP address from your website.
Just paste the codeinto your PHP page.

[PHP]<?php
// This will retrieve ip!
$ip = getenv('REMOTE_ADDR');
// Replace 'x' with ip address!
$blocked = "****.****.****.****";

if (ereg($blocked,$ip))
{
echo "You Have Been Banned";
exit();
}
?>[/PHP]

'echo "You Have Been Banned";' <-- this is the message which will be displayed when the banned ip goes on the page.

Remember to swap the x's with the IP you wish to ban.

Posted: Mon May 07, 2007 11:35 pm
by Flipper3
It is better to use this instead:

Code: Select all

$_SERVER['REMOTE_ADDR']
I read this somewhere on php.net.

Posted: Sun Jun 03, 2007 5:31 am
by rumman
wow... I am amazed, i never thought it could be this easy to block people from entering my website. just 3 or 4 lines of code: I am impressed. thanks.

Posted: Mon Jun 04, 2007 4:15 am
by sakhan
I agree to flipper you should use $_SERVER['REMOTE_ADDR'] instead of get env
as using super global arrays are easy to use and are more powerful and it is also
a recommendation from php in its manual

php

Posted: Tue Aug 14, 2007 12:14 am
by Gebbo
Yeah php is pretty good but i prefer java ;)

Posted: Wed Oct 10, 2007 6:27 pm
by iBye
Could be really uesful, possible to add more IP's?

$blocked = "****.****.****.****, ****.****.****.****";

Or will you get an error ?

Posted: Thu Oct 11, 2007 9:41 am
by Tails5
You'd have to define an array for that, and do a while loop to check if the IP is banned.

Posted: Thu Oct 11, 2007 5:38 pm
by iBye
Tails5 wrote:You'd have to define an array for that, and do a while loop to check if the IP is banned.
So that wont work then? :O

Posted: Thu Oct 11, 2007 11:26 pm
by Tails5
Sure it'll work, it'll just be more complex...

Posted: Sat Oct 13, 2007 8:22 am
by iBye
Tails5 wrote:Sure it'll work, it'll just be more complex...
Oh alright, Im glad I could come up with that idea, I just started to learn php :)