WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a IP restriction plugin but having a strange problem.

I have a textarea in the settings page where each of the IP addresses that should be allowed are entered, one per line. I'm then parsing the IPs into an array like this:

$ips = trim($cmm_options['ips']);   
$ips = array_filter(explode("\n", $ips), 'trim');

This results in array that looks like this:

    [0] =>
    [1] =>

When the page loads, the visitors IP address is checked against this array like this:

function cmm_ip_test($ips){

    //testing that correct IP address used
    for($i = 0, $cnt = count($ips); $i<$cnt; $i++) {
        $ipregex = preg_replace('/\./', '\.', $ips[$i]);
        $ipregex = preg_replace('/\*/', '.*', $ipregex);

        if(preg_match('/'.$ipregex.'/', $_SERVER['REMOTE_ADDR'])){
            // apply filter
            return true;
        //do not apply filter
    return false;

All of this works perfectly fine, with the exception of the first IP in the array. So for example, let's say my IP is and it is the first one enter in the textarea (and so the first one in the array), like this the cmm_ip_test() function will return false. If I then move my IP to the second line (or any other but the first) of the textarea and load the page, cmm_ip_test() returns true.

From all that I can tell, there is absolutely no difference between the first index of the array and any past.

Any ideas why the first index position won't validate, even if it is a valid array?


share|improve this question
up vote 1 down vote accepted

There are two pieces to this code: 1) Interpreting the list of allowed IPs from a textarea, delimited by newline character, and 2) Checking if a given user's IP is in the allowed list of IPs.

Part 1) Parse list of IPs delimited by newline character, and trim whitespace:

$_POST['allowed_ips'] = "    ";

$ips = array_map('trim', explode("\n", $_POST['allowed_ips']));
var_dump($ips); //This should match your Array() output from above

Part 2) Check list of allowed IPs against the user's current IP:

var_dump(in_array($_SERVER['REMOTE_ADDR'], $ips));

in_array() allows you to take a string and check if it exists as a value in an array. Since you have a list of all IPs you want to allow, you just need to check if the user's current IP ($_SERVER['REMOTE_ADDR']) is in the array of allowed IPs.

share|improve this answer
Excellent! I am aware of in_array() and have used it quite a few times. No idea why I didn't think of it for this. It works great. Thanks! – Pippin Oct 11 '11 at 1:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.