Mobile app version of vmapp.org
Login or Join
Phylliss660

: Block IP Address using php For my website i use this php code to block ip addresses. <?php $deny = array("111.111.111", "222.222.222", "333.333.333"); if (in_array ($_SERVER['REMOTE_ADDR'], $deny))

@Phylliss660

Posted in: #IpAddress #Php

For my website i use this php code to block ip addresses.

<?php
$deny = array("111.111.111", "222.222.222", "333.333.333");
if (in_array ($_SERVER['REMOTE_ADDR'], $deny)) {
header("location: www.google.com/ );
exit();
} ?>


How to re-edit this code to block ip addresses like all addresses starting from "111." or "222." like that?

Thanks in advance!

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Phylliss660

1 Comments

Sorted by latest first Latest Oldest Best

 

@Heady270

You can try something like this:

<?
$deny = array("111.111.111", "222.222.222", "333.333.333");
foreach ($deny as $denyip) {
if (strpos($_SERVER['REMOTE_ADDR'], $denyip)===0) {
header("location: www.google.com/ );
exit();
}
}
?>


This basically loops through all denied IP's and checks if the user's IP starts with the IP written in the deny list. It will also work if you put full ip in the deny list, so you can combine full and partial ip's.

Notice the === in comparison. It means that the position must really be 0, which means that user IP begins with the ip in the deny list. If you only put == it will block all users who are not found in the deny list too, since false==0 in php, so that is why you must use === instead.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme