1
<?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
  • Just out of curiosity, do you have error reporting set to E_ALL? Commented Jul 25, 2009 at 6:38

4 Answers 4

8

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

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

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.

1 Comment

I tried moving the variable inside the function and it still has the same error
1

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

Comments

1

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.

5 Comments

Which means that it is a problem with $_SERVER
And for some reason some idiots decided to mark down my answer ... really?
@Chacha102: Sorry, yes, because "try switching arguments" and "trim $_SERVER" is totally unfounded non-sense.
The entire problem was unfound nonsense. He even commented that for some reason it works now.
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.

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.