Friday, March 21, 2008

PHP code to block IP to view your page


Share at Facebook

Suppose your are in a situation that you don't want to view your pages from specific IP address. You can either block them or can redirect to another default page saying "your are not allowed to view the page", or "your blocked :-p". Check the below PHP code.

## Make an array of IP address that you want to block.
$blocked_ip = array("192.168.1.20","192.168.1.21");

## This is the IP address from where request came to you
$request_ip = $_SERVER["HTTP_CLIENT_IP"];

## If the requested IP address exist at blocked IP list
if(array_search($request_ip, $blocked_ip)){
   ## You can stop the script here.
   die();
}

but my suggestion is to redirect to another page instead of die().

Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.example.com/error.html");




No comments: