(PHP) Ban Ip From Your Site

Any help regarding any computer based programming language (non serverside) can be requested and recieved here.
anish
Posts: 353
Joined: Fri Apr 27, 2007 12:34 pm
Contact:

(PHP) Ban Ip From Your Site

Post 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.


Flipper3
Posts: 353
Joined: Tue Feb 28, 2006 12:34 am

Post by Flipper3 »

It is better to use this instead:

Code: Select all

$_SERVER['REMOTE_ADDR']
I read this somewhere on php.net.
rumman
Posts: 15
Joined: Sun Nov 27, 2005 12:58 am

Post 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.
Image
sakhan
Posts: 217
Joined: Sat May 26, 2007 6:02 am

Post 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
Gebbo
Posts: 554
Joined: Tue May 16, 2006 3:22 pm

php

Post by Gebbo »

Yeah php is pretty good but i prefer java ;)
.............................:: Spirit of Fire ::..................................

Image
iBye
Posts: 720
Joined: Mon Jan 29, 2007 1:39 pm

Post by iBye »

Could be really uesful, possible to add more IP's?

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

Or will you get an error ?
Tired of Newbies asking Questions? Tell them www.UseGoogleFFS.com
Tails5
Posts: 1302
Joined: Wed Mar 15, 2006 8:09 am
Contact:

Post by Tails5 »

You'd have to define an array for that, and do a while loop to check if the IP is banned.
Webmaster Yoda: You must confront the cPanel. Then, and only then, a webmaster will you be.
Julius Caesar: Veni, vidi, posti
iBye
Posts: 720
Joined: Mon Jan 29, 2007 1:39 pm

Post 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
Tired of Newbies asking Questions? Tell them www.UseGoogleFFS.com
Tails5
Posts: 1302
Joined: Wed Mar 15, 2006 8:09 am
Contact:

Post by Tails5 »

Sure it'll work, it'll just be more complex...
Webmaster Yoda: You must confront the cPanel. Then, and only then, a webmaster will you be.
Julius Caesar: Veni, vidi, posti
iBye
Posts: 720
Joined: Mon Jan 29, 2007 1:39 pm

Post 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 :)
Tired of Newbies asking Questions? Tell them www.UseGoogleFFS.com
Locked