简体   繁体   English

PHP in_array函数在服务器中不起作用

[英]PHP in_array function not working in server

I use this function and Its Fine on Local means when $hexIpStr exist function return false otherwise return true , But when I Deploy my Project on Server this Function only Return True, $arr is 2D array. 我使用此函数,并且在本地使用它的优良方法意味着$hexIpStr存在时函数返回false否则返回true ,但是当我在服务器上部署项目时,该函数仅返回True,$ arr是2D数组。

function ipChecker($arr, $hexIp)
{
    $hexIpStr = "HEX=" . $hexIp;
    foreach ($arr as $members) {
        if (in_array($hexIpStr, $members)) {
            return false;
        } else {
            return true;
        }
    }
}

The function is for example called with these values: 例如,使用以下值调用该函数:

$hexIp = 'f528764d624db129b32c21fbca0cb8d6';
$arr = array(
  0 => [
    0 => 'FullName=mehdi',
    1 => 'Email=test@gmail.com',
    2 => 'IP=127.0.0.1',
    3 => 'HEX=f528764d624db129b32c21fbca0cb8d6',
    4 => '=>',
    5 => 'Opinion=1 ',
  ],
  1 => [
    0 => '',
  ],
);

no its not answer because its fine in the server i get this 没有它没有答案,因为它在服务器上很好我得到了

FullName=asd Email=kingblack5230@gmail.com IP=192.119.160.197 HEX=dd029394f038b0775138a23df8d9cddd => Opinion=1 and its right and i use this function to get user ip: FullName = asd Email=kingblack5230@gmail.com IP = 192.119.160.197 HEX = dd029394f038b0775138a23df8d9cddd =>意见= 1及其权利,我使用此功能获取用户ip:

function GetRealIp()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
    {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
 }
    return $ip;}

f528764d624db129b32c21fbca0cb8d6 ( # ) is md5('127.0.0.1') , this is likely not the value (IP) you expect / that is in $members . f528764d624db129b32c21fbca0cb8d6 md5('127.0.0.1') ,这可能不是您期望的值(IP)/位于$members You likely expect a public IP. 您可能希望使用公共IP。

If you rely on $_SERVER['REMOTE_ADDR'] this can be the issue. 如果您依赖$_SERVER['REMOTE_ADDR']则可能是问题所在。 If your server is proxying the request to your PHP worker on the same machine (rather than using eg mod_php directly), REMOTE_ADDR will be the IP of the proxy ( 127.0.0.1 / localhost ) rather than the visitor. 如果您的服务器将请求代理到同一台计算机上的PHP工作者(而不是直接使用例如mod_php ),则REMOTE_ADDR将是代理的IP( 127.0.0.1 / localhost ),而不是访问者。

In this case, you should dump $_SERVER and check for a variable HTTP_X_FORWARDED_FOR / use that instead of REMOTE_ADDR , the proxy server will likely add this to include the original IP. 在这种情况下,您应该转储$_SERVER并检查变量HTTP_X_FORWARDED_FOR /使用该变量而不是REMOTE_ADDR ,代理服务器可能会将其添加为包括原始IP。

Your function "returns early" (and thus breaks the foreach ) after checking the first child for your HEX=... string. 在检查第一个孩子的HEX = ...字符串后,您的函数“提早返回”(从而中断了foreach )。 This means you only ever check the first member. 这意味着您只能检查第一个成员。

Given this $arr , your member is in there, just not at the first position: 有了这个$arr ,您的成员就在那里,只是不在第一个位置:

array(
  0 => [
    0 => 'FullName=blub',
    1 => 'Email=blub@gmail.com',
    2 => 'IP=1.2.3.4',
    3 => 'HEX=6465ec74397c9126916786bbcd6d7601',
  ],
  1 => [
    0 => 'FullName=mehdi',
    1 => 'Email=test@gmail.com',
    2 => 'IP=127.0.0.1',
    3 => 'HEX=f528764d624db129b32c21fbca0cb8d6',
  ],
)

It would return true , because only in_array('HEX=f528764d624db129b32c21fbca0cb8d6', ['HEX=6465ec74397c9126916786bbcd6d7601']) is checked. 它将返回true ,因为仅检查了in_array('HEX=f528764d624db129b32c21fbca0cb8d6', ['HEX=6465ec74397c9126916786bbcd6d7601'])

You should restructure your function so it can only " return early" if it found your member, and if not, continues to check all members, and returns the other case only in the end: 您应该重组函数,以便它只能在找到成员后“提前return ”,否则,将继续检查所有成员,并仅在最后返回另一种情况:

function ipChecker($arr, $hexIp)
{
    $hexIpStr = "HEX=" . $hexIp;
    foreach ($arr as $members) {
        if (in_array($hexIpStr, $members)) {
            return false;
        }
    }
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM