0

I need some help. I want to knows is there any possible code in php to block certain ip address from access my IP address and my pc from access certain IP address. (for example, I'm using ip address 192.168.1.2 and I want to block ip address from 192.168.1.3 and vice versa). Is there any possible in php code? if possible, I don't want to write in .htaccess file but code in .php file.

HERE IS MY SOME PHP CODE

<?php   

$address = "192.168.1.7";
$total=0;
$i=1;
$blockIPstatus="no";


while($blockIPstatus=="no") {

$ping = exec("ping $address");
$pingTime = explode(',',trim($ping));
$time = explode("=",trim($pingTime[2]));
$onlytime = implode("m",trim($time));

$number=chop($time[1],"ms");
$total=$total+$number;
$average=$total/$i;

echo ", Total=".$total;
echo ", average=".round($average, 2);   
echo "<br>";

if($average>=500)
        $blockIPstatus="yes";


}

// i need some function to block this ip from here

?>
3
  • Why don't you want to write it in a htaccess file? Commented Dec 21, 2016 at 6:08
  • i just want create a system where user can write ip address in php interface Commented Dec 21, 2016 at 6:13
  • Sorry, my mistake... i already add some my code in php Commented Dec 21, 2016 at 15:06

5 Answers 5

1

There are a lot of way to do it. But if you want to do it with .php, you can do it as follow

         if (!empty($_SERVER['HTTP_CLIENT_IP'])) 
            {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            }
            elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) 
            {
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            } 
            else 
            {
                $ip = $_SERVER['REMOTE_ADDR'];
            }

            if($ip =='192.168.1.2')
            {
                echo "Not allowed"; 
                exit;
            }
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

Order Deny,Allow
Deny from 192.168.1.3

Explanation:

The visitor blocking facilities offered by the Apache Web Server enable us to deny access to specific visitors, or allow access to specific visitors. This is extremely useful for blocking unwanted visitors, or to only allow the web site owner access to certain sections of the web site, such as an administration area.

To set-up visitors restrictions and blocking, create a .htaccess file following the main instructions and guidance which includes the following text:

order allow,deny
deny from 255.0.0.0
deny from 123.45.6.
allow from all

The above lines tell the Apache Web Server to block visitors from the IP address '255.0.0.0' and '123.45.6.', note the second IP address is missing the fourth set of digits, this means any IP address which matches the firth three set of digits will be blocked, e.g. '123.45.6.10' and '123.45.6.255' would be blocked.

Reference

1 Comment

is this block can prevent my pc from access that ip address or it is only prevent ip address in web system only.. if possible,, i want my pc cannot ping that ip address.. is that possible ?
0

Try to something like this.

if($_SERVER['REMOTE_ADDR'] == "192.168.1.3"))
{
    die("This IP is Blocked");
}

1 Comment

is this block can prevent my pc from access that ip address, for example, i want my pc cannot ping that ip address.. is that possible ?
0

If you want to block IP in your PHP code, you can use follow code

 function getip()
 {
      if (getenv("HTTP_CLIENT_IP") &&      strcasecmp(getenv("HTTP_CLIENT_IP"),"unknown"))
      $ip = getenv("HTTP_CLIENT_IP");

      elseif (getenv("HTTP_X_FORWARDED_FOR") &&      strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
      $ip = getenv("HTTP_X_FORWARDED_FOR");

      elseif (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
      $ip = getenv("REMOTE_ADDR");

      elseif (!empty($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
      $ip = $_SERVER['REMOTE_ADDR'];

      else
      $ip = "unknown";

      return($ip);
 }

Example of use this function

 if (getip()=="10.0.0.01") die("Your IP address is blocked");

Comments

0

Using .htaccess file you are able to block IP's

like this,

add this line in your .htaccess file

Order Deny,Allow

Deny from 192.168.1.2

OR It can be done by PHP side also

<?php
   if(strpos($_SERVER['REMOTE_ADDR'],"192.168.1.2") === 0){
     echo "IP: Blocked";die();
   }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.