PHP Freelancer

Freelance PHP Developer

Block IP addresses using .htaccess or .php file

Posted on | March 21, 2009 | 11 Comments

Hello Friends ,

If you are looking to block some of your visitors by their IP address here are the some way that might help you.

1. Using .htaccess

If you want to block IP address using .htaccess file than just copy the below code and paste it in to your .htaccess file. You just need to change the IP address that you want to block. Replace 120.120.120.120 with the IP address that you want to block.

order deny,allow
allow from all
deny from 120.120.120.120

If you want to block series of IP address than just copy the below code and paste it in to your .htaccess file. And replace your IP address series. As per example given deny from 120.120.120 means it blocks 120.120.120.0 , 120.120.120.1 , .. , 120.120.120.255 IP addresses (means total 256 IP addresses).

order deny,allow
allow from all
deny from 120.120.120.

In some cases if you want to block bunch of IP address than just copy the below code and paste it in to your .htaccess file. And replace your IP address bunch as per yours . As per example given deny from 120.120.*. means it blocks 120.120.*.0 , 120.120.*.1 , .. , 120.120.255.255 IP addresses (means total 256 * 256 = 65536 IP addresses).

order deny,allow
allow from all
deny from 120.120.*.


2 . Using php file

If you don’t have .htaccess file access and want to block IP address using .php file than just copy the below code and paste it in to your .php file. You just need to change the IP address that you want to block. Replace 120.120.120.120 , 120.120.120.121 , 120.120.120.122 with the IP addresses that you want to block. You can define as many IP address as you want to block in an array declared below.

<?php
$block = array(“120.120.120.120″, “120.120.120.121″, “120.120.120.122″);
if (in_array ($_SERVER['REMOTE_ADDR'], $block))
{
header(“location: http://www.yoursite.com/404.php?error=notallow”);
exit();
}
?>

If you want to generate  .htaccess file than click here .

Cheers !!

      
Plugin by: PHP Freelancer

Comments

11 Responses to “Block IP addresses using .htaccess or .php file”

  1. all about loan
    April 5th, 2009 @ 1:16 pm

    thanks for your information, i’ll try it in my blog

  2. jocuri
    June 4th, 2009 @ 7:50 am

    very good info
    thank you
    jocuri

  3. Fall
    August 5th, 2009 @ 11:03 am

    In 2001 Terry Semel then took a CEO position at the Search Engine and Media giant Yahoo.;

  4. Gluettony
    August 5th, 2009 @ 11:33 am

    The good news is that these issues can be avoided by choosing the right insurance for tourist policy.;

  5. Mark28
    October 22nd, 2009 @ 1:52 pm

    nice one. helped me lot

  6. Passion_avenue
    February 20th, 2011 @ 2:51 pm

    i am really a begginer. these idea sharing help me a lot. Thanks!

  7. Anonymous
    February 20th, 2011 @ 2:54 pm

    order deny,allow
    allow from all
    deny from 120.120.*.

    cool job, man

  8. Dating
    February 20th, 2011 @ 2:58 pm

    .htaccess is not in my directory, just htaccess.txt.
    Do i rename it?

  9. Brinkka
    April 25th, 2011 @ 8:39 am

    if I want to deny from all and allow the ip with the ip prefix 127.0. thanks.

  10. justhostweb
    July 14th, 2011 @ 2:41 am

    justhost hosting best hosting.

  11. justhost web
    July 22nd, 2011 @ 12:44 am

    justhostt web hosting. wvxprp raknk ftap

Leave a Reply





Freelance PHP Developer