: 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))
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!
More posts by @Phylliss660
1 Comments
Sorted by latest first Latest Oldest Best
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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.