<?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();
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
$bannedIPsinside your function as such:Tell your function to access
$bannedIPsoutside the function using theglobalkeyword:Or, use the
$GLOBALSsuper global: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$bannedIPsbefore down voting us all.