tags:

views:

228

answers:

4
+1  Q: 

php in array error

<?PHP
$bannedIPs = array('127.0.0.1','72.189.218.85');

function ipban() {
    if (in_array($_SERVER['REMOTE_ADDR'], $bannedIPs)) { 
     echo 'test';
    } 
}

ipban();

?>

The output of this script is:

Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\webserver\htdocs\test\array.php on line 93

Can someone tell me why? I don't get it

And yes $_SERVER['REMOTE_ADDR'] is displaying 127.0.0.1

UPDATE As suggested I tried this now but still get the same error

function ipban() {
    $bannedIPs = array('127.0.0.1','72.189.218.85');
    if (in_array($_SERVER['REMOTE_ADDR'], $bannedIPs)) { 
     echo 'test';
    } 
}
ipban();
+1  A: 

What happens if you move $bannedIPs inside the function declaration? It's possible PHP thinks it's out of scope.

Tim
I did this but I still get the same error
jasondavis
+1  A: 

You need to global $bannedIPs;

This works for me:

function ipban() {
    $bannedIPs = array('127.0.0.1','72.189.218.85');
    $ip = '127.0.0.1';
    if (in_array($ip, $bannedIPs)) { 
        echo 'test';
    }   
}
ipban();

So, you might want to see if that works, substitute in the IP address, and then finally replace it with the SERVER variable.

Chacha102
amazing that didn't work either, I am stumped
jasondavis
Tried the above code on my server, and it works fine.
Chacha102
Which means that it is a problem with $_SERVER
Chacha102
working now strange, thanks though
jasondavis
And for some reason some idiots decided to mark down my answer ... really?
Chacha102
@Chacha102: Sorry, yes, because "try switching arguments" and "trim `$_SERVER`" is totally unfounded non-sense.
deceze
The entire problem was unfound nonsense. He even commented that for some reason it works now.
Chacha102
If you'd like, I can remove all the unfound nonsense from my answer...
Chacha102
The original question was a perfectly valid scope problem, as the beginning of your answer says. The rest of your answer is bad advise. Feel free to edit it.
deceze
+2  A: 

Your variable $bannedIPs is out-of-scope inside the function. Read up on variables scope: http://php.net/variables.scope

$var = 'xyz';

function abc() {
    // $var does not exist here

    $foo = 'abc';
}

// $var exists here

// $foo does not exist here

RE: Update:

Moving the variable inside the function works, your code snippet executes fine. There's got to be something else in your code.

deceze
I tried moving the variable inside the function and it still has the same error
jasondavis
+7  A: 

You have run into a little problem with your variable scoping.

Any variables outside a function in PHP is not accessible inside. There are multiple ways to overcome this.

You could either declare $bannedIPs inside your function as such:

function ipban() {
    $bannedIPs = array('127.0.0.1','72.189.218.85');
    if (in_array($_SERVER['REMOTE_ADDR'], $bannedIPs)) { 
        echo 'test';
    }   
}

Tell your function to access $bannedIPs outside the function using the global keyword:

$bannedIPs = array('127.0.0.1','72.189.218.85');

function ipban() {
    global $bannedIPs;

    if (in_array($_SERVER['REMOTE_ADDR'], $bannedIPs)) { 
        echo 'test';
    }
}

Or, use the $GLOBALS super global:

$bannedIPs = array('127.0.0.1','72.189.218.85');

function ipban() {
    if (in_array($_SERVER['REMOTE_ADDR'], $GLOBALS['bannedIPs'])) { 
        echo 'test';
    }
}

I recommend you read the manual page on Variable scope:

PHP: Variable Scope


If it's still not working, you have another problem in your code. In which case, you might want to consider using a var_dump() to check what datatype is $bannedIPs before down voting us all.

function ipban() {
    global $bannedIPs;

    var_dump($bannedIPs);
}
Andrew Moore
+1, especially for the downvote part. This is turning into a kindergarten thread.
deceze
Another way I found is by passing the variable into the function on call ipban($bannedIPs);
jasondavis
**@jasondavis:** Yes, but then you are making your function call more verbose for nothing. It's code smell.
Andrew Moore