Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How To Ban A Specific IP From Accessing Your Site With PHP
#1
I was just messing around with a little PHP, and I thought of this way to block access to certain IP addresses from accessing your web page. I originally made it to make it so people who aren't me cannot view a certain page, but just changed it from if not to if and there you have it.

PHP Code:
<html>
<
head>
<
title>Banned IP</title>
</
head>

<?
php
If ($_SERVER['REMOTE_ADDR'] == "1.1.1.1"){ // Replace 1.1.1.1 with the IP you want to block access
echo 'Your IP is banned from accessing this webpage.<br /><br />';
}
?>
</html> 
Reply
#2
(01-11-2012, 03:08 PM)BreShiE Wrote: I was just messing around with a little PHP, and I thought of this way to block access to certain IP addresses from accessing your web page. I originally made it to make it so people who aren't me cannot view a certain page, but just changed it from if not to if and there you have it.

PHP Code:
<html>
<
head>
<
title>Banned IP</title>
</
head>

<?
php
If ($_SERVER['REMOTE_ADDR'] == "1.1.1.1"){ // Replace 1.1.1.1 with the IP you want to block access
echo 'Your IP is banned from accessing this webpage.<br /><br />';
}
?>
</html> 

You're going to add this manually for every IP you have to ban though? Huh

You could also go with an .htaccess method
Reply
#3
You could just ban the IP in your cPanel, if you use cPanel.
Reply
#4
(01-11-2012, 07:27 PM)lovetospooge Wrote: You could just ban the IP in your cPanel, if you use cPanel.

But he is telling us how to do it with PHP.
Reply
#5
(01-11-2012, 05:33 PM)AceInfinity Wrote: You're going to add this manually for every IP you have to ban though? Huh

You could also go with an .htaccess method

This is just a quick way of banning people from one page, if you wanted to do it for all of them you can make that a lot easier too. Put the code into a file called something like 'inc.php' then just:

PHP Code:
<?php
require 'inc.php'// Do this for every page, and you can copy and paste it to make it even quicker.
?>

Simples.

(01-11-2012, 07:32 PM)Laugh Wrote:
(01-11-2012, 07:27 PM)lovetospooge Wrote: You could just ban the IP in your cPanel, if you use cPanel.

But he is telling us how to do it with PHP.

Exactly, thanks.
Reply
#6
Title should have had php in it perhaps.
To stop people saying .htaccess as thats the better option i feel.
But a good post. Update the thread title i think mate.

The Rules!
FTW Forum <-- Home of the Damned! --> Join me On MM


Reply
#7
Edited the title and included PHP in it, hopefully people wont get the wrong idea now.
Reply
#8
Even though, I love the .htaccess method.
Reply
#9
great share mate.
i didn't know that.
thanks.
[Image: 3dtextven0mhf.png]
Reply
#10
You are improving BreShiE, however I have some words of advice for you...

In today's ISP world, most home systems change their IP's quite frequently, therefore you would need to manually track and log each access and figure out which IP is related to your blocked one... This simply means, if you block my IP, a router/system restart would be all I need to do in order to gain access to your site again, the workaround would be to Block a Hostname instead of an IP, since that hostname will remain same even once IP has changed... To do this just get the host name using the function gethostbyaddr(string $ip)...
http://php.net/manual/en/function.gethostbyaddr.php

It can occur that different users share the Hostname, meaning they would be also blocked, however, imo it's better to block the host name instead of a dynamic IP.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to track bandwidth for specific files and pages? dipped9900 1 861 07-14-2012, 05:25 PM
Last Post: 'Snorlax
  [PHP] - PHP White Paper --([-S7N-])-- 1 1,062 04-13-2010, 09:42 PM
Last Post: SLiM

Forum Jump:


Users browsing this thread: 4 Guest(s)