Block specific IP Addresses Website Access by Country using PHP

Block specific IP Addresses Website Access by Country using PHP

In this Post We Will Explain About is With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to block an IP address from visiting a website using PHPExample

In this post we will show you Best way to implement Block specific IP block from my website in PHP, hear for How To Block An IP Address From Visiting A Websitewith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Here simple way to First, we will make a an array to list all the list of IP addresses that we want to block of list. Besides single check IP addresses, we will simple also use get and IP ranges such as Like as a 141.64.197.101-141.64.197.199 and create bunch of the IP masks 192.88.*.*. Here is simple PHP the array

Also Read :  Upload video files Images using PHP mysql

<?php
$my_fetchip = array(
        '111.65.248.132',
        '141.64.197.119',       
        '192.168.1.192',
        '192.168.1.105',
        '192.88.*.*',
        '122.25.192.*',
        '141.64.197.101-141.64.197.199',
        '98.255.255.192-98.255.255.150'
);

Generally a visitor comes to wer any website we will need to all the ip find their IP address as well as store it in a simple variable. We can find out simple the IP address using this data of the piece of Source code:

if (isset($_SERVER['HTTP_CLIENT_IP']))
{
    $get_liveip_client = $_SERVER['HTTP_CLIENT_IP'];
} else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $get_liveip_client = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if(isset($_SERVER['HTTP_X_FORWARDED'])) {
    $get_liveip_client = $_SERVER['HTTP_X_FORWARDED'];
} else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) {
    $get_liveip_client = $_SERVER['HTTP_FORWARDED_FOR'];
} else if(isset($_SERVER['HTTP_FORWARDED'])) {
    $get_liveip_client = $_SERVER['HTTP_FORWARDED'];
} else if(isset($_SERVER['REMOTE_ADDR'])) {
    $get_liveip_client = $_SERVER['REMOTE_ADDR'];
}

$ip_blocked = false;
foreach($my_fetchip as $ip)
        {
            if($get_liveip_client == $ip)
            {
                $ip_blocked = true;
                break;
            }else if(strpos($ip, '*') !== false){
                $get_digits = explode(".", $ip);
                $get_liveip_client_get_digits = explode(".", $get_liveip_client);
                if($get_digits[1] == '*' && $get_digits[0] == $get_liveip_client_get_digits[0])
                {
                    $ip_blocked = true;
                    break;
                }else if($get_digits[2] == '*' && $get_digits[0] == $get_liveip_client_get_digits[0] && $get_digits[1] == $get_liveip_client_get_digits[1]){
                    $ip_blocked = true;
                    break;
                }else if($get_digits[3] == '*' && $get_digits[0] == $get_liveip_client_get_digits[0] && $get_digits[1] == $get_liveip_client_get_digits[1] && $get_digits[2] == $get_liveip_client_get_digits[2]){
                    $ip_blocked = true;
                    break;
                }
            }else if(strpos($ip, "-") !== false){
                list($start_ip, $end_ip) = explode("-", $ip);
                $start_ip = preg_replace('/\s+/', '', $start_ip);
                $end_ip = preg_replace('/\s+/', '', $end_ip);
                 
                $start_ip_long = ip2long($start_ip);
                $end_ip_long = ip2long($end_ip);
                 
                if($get_liveip_client_long >= $start_ip_long && $get_liveip_client_long <= $end_ip_long)
                {
                    $ip_blocked = true;
                    break;
                }
            }
        }

simple, and then At the end we will simple way to the have a variable $ip_blocked which if data set to true means that get all the visitors IP address is found in simple our list with ip_blocked IP addresses and some data and we should data ip address block it.

Also Read :  WordPress Security Tricks to Secure WordPress Website

if($ip_blocked == true)
    {
        header('Location: ip_blocked-livepage.html');
    }

We can redirect the user to redirect another page or just simple print some error message.

Example

I hope you have Got What is How to restrict access to a web page by user’s IP address And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Recommended For You.

PHP Webcam Capture Image Save from Camera using jQuery
Today, We want to share with you PHP Webcam Capture Image Save from Camera using jQuery.In this post we will