Support Forums
How To Ban A Specific IP From Accessing Your Site With PHP - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Webmaster Support (https://www.supportforums.net/forumdisplay.php?fid=36)
+---- Forum: Website Development (https://www.supportforums.net/forumdisplay.php?fid=43)
+---- Thread: How To Ban A Specific IP From Accessing Your Site With PHP (/showthread.php?tid=24493)

Pages: 1 2


How To Ban A Specific IP From Accessing Your Site With PHP - BreShiE - 01-11-2012

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> 



RE: How To Ban A Specific IP From Accessing Your Site - AceInfinity - 01-11-2012

(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


RE: How To Ban A Specific IP From Accessing Your Site - lovetospooge - 01-11-2012

You could just ban the IP in your cPanel, if you use cPanel.


RE: How To Ban A Specific IP From Accessing Your Site - Peter L - 01-11-2012

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


RE: How To Ban A Specific IP From Accessing Your Site - BreShiE - 01-12-2012

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


RE: How To Ban A Specific IP From Accessing Your Site - DAMINKā„¢ - 01-12-2012

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.




RE: How To Ban A Specific IP From Accessing Your Site With PHP - BreShiE - 01-12-2012

Edited the title and included PHP in it, hopefully people wont get the wrong idea now.


RE: How To Ban A Specific IP From Accessing Your Site With PHP - Techie. - 01-12-2012

Even though, I love the .htaccess method.


RE: How To Ban A Specific IP From Accessing Your Site With PHP - ven0m - 01-12-2012

great share mate.
i didn't know that.
thanks.


RE: How To Ban A Specific IP From Accessing Your Site With PHP - Gaijin - 01-12-2012

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.